blob: a1441ac16317d98f7a2b5cb4620c16c9b8757848 [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") \
Leon Clarked91b9f72010-01-27 17:25:45 +000094 V(FUNCTION_CREATION_EVENT, "function-creation", "fc") \
95 V(FUNCTION_MOVE_EVENT, "function-move", "fm") \
96 V(FUNCTION_DELETE_EVENT, "function-delete", "fd") \
Leon Clarkee46be812010-01-19 14:06:41 +000097 V(SNAPSHOT_POSITION_EVENT, "snapshot-pos", "sp") \
Steve Blocka7e24c12009-10-30 11:49:00 +000098 V(TICK_EVENT, "tick", "t") \
99 V(REPEAT_META_EVENT, "repeat", "r") \
100 V(BUILTIN_TAG, "Builtin", "bi") \
101 V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak", "cdb") \
102 V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn", "cdbsi") \
103 V(CALL_IC_TAG, "CallIC", "cic") \
104 V(CALL_INITIALIZE_TAG, "CallInitialize", "ci") \
105 V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic", "cmm") \
106 V(CALL_MISS_TAG, "CallMiss", "cm") \
107 V(CALL_NORMAL_TAG, "CallNormal", "cn") \
108 V(CALL_PRE_MONOMORPHIC_TAG, "CallPreMonomorphic", "cpm") \
Steve Blockd0582a62009-12-15 09:54:21 +0000109 V(CALLBACK_TAG, "Callback", "cb") \
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 V(EVAL_TAG, "Eval", "e") \
111 V(FUNCTION_TAG, "Function", "f") \
112 V(KEYED_LOAD_IC_TAG, "KeyedLoadIC", "klic") \
113 V(KEYED_STORE_IC_TAG, "KeyedStoreIC", "ksic") \
114 V(LAZY_COMPILE_TAG, "LazyCompile", "lc") \
115 V(LOAD_IC_TAG, "LoadIC", "lic") \
116 V(REG_EXP_TAG, "RegExp", "re") \
117 V(SCRIPT_TAG, "Script", "sc") \
118 V(STORE_IC_TAG, "StoreIC", "sic") \
Steve Block6ded16b2010-05-10 14:33:55 +0100119 V(STUB_TAG, "Stub", "s") \
120 V(NATIVE_FUNCTION_TAG, "Function", "f") \
121 V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile", "lc") \
122 V(NATIVE_SCRIPT_TAG, "Script", "sc")
123// Note that 'NATIVE_' cases for functions and scripts are mapped onto
124// original tags when writing to the log.
125
Steve Blocka7e24c12009-10-30 11:49:00 +0000126
127class Logger {
128 public:
129#define DECLARE_ENUM(enum_item, ignore1, ignore2) enum_item,
130 enum LogEventsAndTags {
131 LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM)
132 NUMBER_OF_LOG_EVENTS
133 };
134#undef DECLARE_ENUM
135
136 // Acquires resources for logging if the right flags are set.
137 static bool Setup();
138
139 // Frees resources acquired in Setup.
140 static void TearDown();
141
142 // Enable the computation of a sliding window of states.
143 static void EnableSlidingStateWindow();
144
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 // Emits an event with a string value -> (name, value).
146 static void StringEvent(const char* name, const char* value);
147
148 // Emits an event with an int value -> (name, value).
149 static void IntEvent(const char* name, int value);
150
151 // Emits an event with an handle value -> (name, location).
152 static void HandleEvent(const char* name, Object** location);
153
154 // Emits memory management events for C allocated structures.
155 static void NewEvent(const char* name, void* object, size_t size);
156 static void DeleteEvent(const char* name, void* object);
157
158 // Emits an event with a tag, and some resource usage information.
159 // -> (name, tag, <rusage information>).
160 // Currently, the resource usage information is a process time stamp
161 // and a real time timestamp.
162 static void ResourceEvent(const char* name, const char* tag);
163
164 // Emits an event that an undefined property was read from an
165 // object.
166 static void SuspectReadEvent(String* name, Object* obj);
167
168 // Emits an event when a message is put on or read from a debugging queue.
169 // DebugTag lets us put a call-site specific label on the event.
170 static void DebugTag(const char* call_site_tag);
171 static void DebugEvent(const char* event_type, Vector<uint16_t> parameter);
172
173
174 // ==== Events logged by --log-api. ====
175 static void ApiNamedSecurityCheck(Object* key);
176 static void ApiIndexedSecurityCheck(uint32_t index);
177 static void ApiNamedPropertyAccess(const char* tag,
178 JSObject* holder,
179 Object* name);
180 static void ApiIndexedPropertyAccess(const char* tag,
181 JSObject* holder,
182 uint32_t index);
183 static void ApiObjectAccess(const char* tag, JSObject* obj);
184 static void ApiEntryCall(const char* name);
185
186
187 // ==== Events logged by --log-code. ====
Steve Blockd0582a62009-12-15 09:54:21 +0000188 // Emits a code event for a callback function.
189 static void CallbackEvent(String* name, Address entry_point);
190 static void GetterCallbackEvent(String* name, Address entry_point);
191 static void SetterCallbackEvent(String* name, Address entry_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 // Emits a code create event.
193 static void CodeCreateEvent(LogEventsAndTags tag,
194 Code* code, const char* source);
195 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, String* name);
196 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, String* name,
197 String* source, int line);
198 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count);
199 // Emits a code create event for a RegExp.
200 static void RegExpCodeCreateEvent(Code* code, String* source);
201 // Emits a code move event.
202 static void CodeMoveEvent(Address from, Address to);
203 // Emits a code delete event.
204 static void CodeDeleteEvent(Address from);
Leon Clarked91b9f72010-01-27 17:25:45 +0000205 // Emits a function object create event.
206 static void FunctionCreateEvent(JSFunction* function);
207 // Emits a function move event.
208 static void FunctionMoveEvent(Address from, Address to);
209 // Emits a function delete event.
210 static void FunctionDeleteEvent(Address from);
Steve Blocka7e24c12009-10-30 11:49:00 +0000211
Leon Clarkee46be812010-01-19 14:06:41 +0000212 static void SnapshotPositionEvent(Address addr, int pos);
213
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 // ==== Events logged by --log-gc. ====
215 // Heap sampling events: start, end, and individual types.
216 static void HeapSampleBeginEvent(const char* space, const char* kind);
217 static void HeapSampleEndEvent(const char* space, const char* kind);
218 static void HeapSampleItemEvent(const char* type, int number, int bytes);
219 static void HeapSampleJSConstructorEvent(const char* constructor,
220 int number, int bytes);
221 static void HeapSampleJSRetainersEvent(const char* constructor,
222 const char* event);
Steve Block3ce2e202009-11-05 08:53:23 +0000223 static void HeapSampleJSProducerEvent(const char* constructor,
224 Address* stack);
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 static void HeapSampleStats(const char* space, const char* kind,
226 int capacity, int used);
227
228 static void SharedLibraryEvent(const char* library_path,
229 uintptr_t start,
230 uintptr_t end);
231 static void SharedLibraryEvent(const wchar_t* library_path,
232 uintptr_t start,
233 uintptr_t end);
234
235 // ==== Events logged by --log-regexp ====
236 // Regexp compilation and execution events.
237
238 static void RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache);
239
240 // Log an event reported from generated code
241 static void LogRuntime(Vector<const char> format, JSArray* args);
242
243#ifdef ENABLE_LOGGING_AND_PROFILING
Steve Blocka7e24c12009-10-30 11:49:00 +0000244 static bool is_logging() {
Steve Block6ded16b2010-05-10 14:33:55 +0100245 return logging_nesting_ > 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000246 }
247
248 // Pause/Resume collection of profiling data.
249 // When data collection is paused, CPU Tick events are discarded until
250 // data collection is Resumed.
Andrei Popescu402d9372010-02-26 13:31:12 +0000251 static void PauseProfiler(int flags, int tag);
252 static void ResumeProfiler(int flags, int tag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 static int GetActiveProfilerModules();
254
255 // If logging is performed into a memory buffer, allows to
256 // retrieve previously written messages. See v8.h.
257 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
258
259 // Logs all compiled functions found in the heap.
260 static void LogCompiledFunctions();
Leon Clarked91b9f72010-01-27 17:25:45 +0000261 // Logs all compiled JSFunction objects found in the heap.
262 static void LogFunctionObjects();
Steve Blockd0582a62009-12-15 09:54:21 +0000263 // Logs all accessor callbacks found in the heap.
264 static void LogAccessorCallbacks();
265 // Used for logging stubs found in the snapshot.
Andrei Popescu31002712010-02-23 13:46:05 +0000266 static void LogCodeObjects();
Steve Blocka7e24c12009-10-30 11:49:00 +0000267
Steve Block6ded16b2010-05-10 14:33:55 +0100268 // Converts tag to a corresponding NATIVE_... if the script is native.
269 INLINE(static LogEventsAndTags ToNativeByScript(LogEventsAndTags, Script*));
Steve Blocka7e24c12009-10-30 11:49:00 +0000270
271 // Profiler's sampling interval (in milliseconds).
272 static const int kSamplingIntervalMs = 1;
273
Steve Block6ded16b2010-05-10 14:33:55 +0100274 private:
275
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 // Size of window used for log records compression.
277 static const int kCompressionWindowSize = 4;
278
279 // Emits the profiler's first message.
280 static void ProfilerBeginEvent();
281
Steve Blockd0582a62009-12-15 09:54:21 +0000282 // Emits callback event messages.
283 static void CallbackEventInternal(const char* prefix,
284 const char* name,
285 Address entry_point);
286
Leon Clarked91b9f72010-01-27 17:25:45 +0000287 // Internal configurable move event.
288 static void MoveEventInternal(LogEventsAndTags event,
289 Address from,
290 Address to);
291
292 // Internal configurable move event.
293 static void DeleteEventInternal(LogEventsAndTags event,
294 Address from);
295
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 // Emits aliases for compressed messages.
297 static void LogAliases();
298
299 // Emits the source code of a regexp. Used by regexp events.
300 static void LogRegExpSource(Handle<JSRegExp> regexp);
301
Andrei Popescu31002712010-02-23 13:46:05 +0000302 // Used for logging stubs found in the snapshot.
303 static void LogCodeObject(Object* code_object);
304
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 // Emits a profiler tick event. Used by the profiler thread.
306 static void TickEvent(TickSample* sample, bool overflow);
307
308 static void ApiEvent(const char* name, ...);
309
310 // Logs a StringEvent regardless of whether FLAG_log is true.
311 static void UncheckedStringEvent(const char* name, const char* value);
312
Steve Block6ded16b2010-05-10 14:33:55 +0100313 // Logs an IntEvent regardless of whether FLAG_log is true.
314 static void UncheckedIntEvent(const char* name, int value);
315
Steve Blocka7e24c12009-10-30 11:49:00 +0000316 // Stops logging and profiling in case of insufficient resources.
317 static void StopLoggingAndProfiling();
318
319 // Returns whether profiler's sampler is active.
320 static bool IsProfilerSamplerActive();
321
322 // The sampler used by the profiler and the sliding state window.
323 static Ticker* ticker_;
324
325 // When the statistical profile is active, profiler_
326 // points to a Profiler, that handles collection
327 // of samples.
328 static Profiler* profiler_;
329
Steve Blocka7e24c12009-10-30 11:49:00 +0000330 // SlidingStateWindow instance keeping a sliding window of the most
331 // recent VM states.
332 static SlidingStateWindow* sliding_state_window_;
333
334 // An array of log events names.
335 static const char** log_events_;
336
337 // An instance of helper created if log compression is enabled.
338 static CompressionHelper* compression_helper_;
339
340 // Internal implementation classes with access to
341 // private members.
342 friend class CompressionHelper;
343 friend class EventLog;
344 friend class TimeLog;
345 friend class Profiler;
346 friend class SlidingStateWindow;
Steve Blockd0582a62009-12-15 09:54:21 +0000347 friend class StackTracer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 friend class VMState;
349
350 friend class LoggerTestHelper;
351
Steve Block6ded16b2010-05-10 14:33:55 +0100352 static int logging_nesting_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000353 static int cpu_profiler_nesting_;
354 static int heap_profiler_nesting_;
Steve Block6ded16b2010-05-10 14:33:55 +0100355
356 friend class CpuProfiler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000357#else
358 static bool is_logging() { return false; }
359#endif
360};
361
362
363// Class that extracts stack trace, used for profiling.
364class StackTracer : public AllStatic {
365 public:
366 static void Trace(TickSample* sample);
367};
368
Steve Blocka7e24c12009-10-30 11:49:00 +0000369} } // namespace v8::internal
370
Steve Block6ded16b2010-05-10 14:33:55 +0100371
Steve Blocka7e24c12009-10-30 11:49:00 +0000372#endif // V8_LOG_H_