blob: 6ffd18c61bedea97133e73c6067c1df2ecec507d [file] [log] [blame]
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
lrn@chromium.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
ager@chromium.org3e875802009-06-29 08:26:34 +000032#include "platform.h"
33#include "log-utils.h"
34
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
38// Logger is used for collecting logging information from V8 during
39// execution. The result is dumped to a file.
40//
41// Available command line flags:
42//
43// --log
44// Minimal logging (no API, code, or GC sample events), default is off.
45//
46// --log-all
47// Log all events to the file, default is off. This is the same as combining
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000048// --log-api, --log-code, --log-gc, and --log-regexp.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049//
50// --log-api
51// Log API events to the logfile, default is off. --log-api implies --log.
52//
53// --log-code
54// Log code (create, move, and delete) events to the logfile, default is off.
55// --log-code implies --log.
56//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057// --log-gc
58// Log GC heap samples after each GC that can be processed by hp2ps, default
59// is off. --log-gc implies --log.
60//
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000061// --log-regexp
62// Log creation and use of regular expressions, Default is off.
63// --log-regexp implies --log.
64//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065// --logfile <filename>
66// Specify the name of the logfile, default is "v8.log".
67//
68// --prof
69// Collect statistical profiling information (ticks), default is off. The
70// tick profiler requires code events, so --prof implies --log-code.
71
72// Forward declarations.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000073class HashMap;
74class LogMessageBuilder;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075class Profiler;
76class Semaphore;
77class SlidingStateWindow;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000078class Ticker;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079
80#undef LOG
81#ifdef ENABLE_LOGGING_AND_PROFILING
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082#define LOG(isolate, Call) \
83 do { \
84 v8::internal::Logger* logger = \
85 (isolate)->logger(); \
86 if (logger->is_logging()) \
87 logger->Call; \
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000088 } while (false)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089#else
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090#define LOG(isolate, Call) ((void) 0)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091#endif
92
ager@chromium.orgea91cc52011-05-23 06:06:11 +000093#define LOG_EVENTS_AND_TAGS_LIST(V) \
94 V(CODE_CREATION_EVENT, "code-creation") \
95 V(CODE_MOVE_EVENT, "code-move") \
96 V(CODE_DELETE_EVENT, "code-delete") \
97 V(CODE_MOVING_GC, "code-moving-gc") \
98 V(SHARED_FUNC_MOVE_EVENT, "sfi-move") \
99 V(SNAPSHOT_POSITION_EVENT, "snapshot-pos") \
100 V(SNAPSHOT_CODE_NAME_EVENT, "snapshot-code-name") \
101 V(TICK_EVENT, "tick") \
102 V(REPEAT_META_EVENT, "repeat") \
103 V(BUILTIN_TAG, "Builtin") \
104 V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak") \
105 V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn") \
106 V(CALL_IC_TAG, "CallIC") \
107 V(CALL_INITIALIZE_TAG, "CallInitialize") \
108 V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic") \
109 V(CALL_MISS_TAG, "CallMiss") \
110 V(CALL_NORMAL_TAG, "CallNormal") \
111 V(CALL_PRE_MONOMORPHIC_TAG, "CallPreMonomorphic") \
112 V(KEYED_CALL_DEBUG_BREAK_TAG, "KeyedCallDebugBreak") \
113 V(KEYED_CALL_DEBUG_PREPARE_STEP_IN_TAG, \
114 "KeyedCallDebugPrepareStepIn") \
115 V(KEYED_CALL_IC_TAG, "KeyedCallIC") \
116 V(KEYED_CALL_INITIALIZE_TAG, "KeyedCallInitialize") \
117 V(KEYED_CALL_MEGAMORPHIC_TAG, "KeyedCallMegamorphic") \
118 V(KEYED_CALL_MISS_TAG, "KeyedCallMiss") \
119 V(KEYED_CALL_NORMAL_TAG, "KeyedCallNormal") \
120 V(KEYED_CALL_PRE_MONOMORPHIC_TAG, "KeyedCallPreMonomorphic") \
121 V(CALLBACK_TAG, "Callback") \
122 V(EVAL_TAG, "Eval") \
123 V(FUNCTION_TAG, "Function") \
124 V(KEYED_LOAD_IC_TAG, "KeyedLoadIC") \
125 V(KEYED_LOAD_MEGAMORPHIC_IC_TAG, "KeyedLoadMegamorphicIC") \
126 V(KEYED_EXTERNAL_ARRAY_LOAD_IC_TAG, "KeyedExternalArrayLoadIC") \
127 V(KEYED_STORE_IC_TAG, "KeyedStoreIC") \
128 V(KEYED_STORE_MEGAMORPHIC_IC_TAG, "KeyedStoreMegamorphicIC") \
129 V(KEYED_EXTERNAL_ARRAY_STORE_IC_TAG, "KeyedExternalArrayStoreIC") \
130 V(LAZY_COMPILE_TAG, "LazyCompile") \
131 V(LOAD_IC_TAG, "LoadIC") \
132 V(REG_EXP_TAG, "RegExp") \
133 V(SCRIPT_TAG, "Script") \
134 V(STORE_IC_TAG, "StoreIC") \
135 V(STUB_TAG, "Stub") \
136 V(NATIVE_FUNCTION_TAG, "Function") \
137 V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile") \
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000138 V(NATIVE_SCRIPT_TAG, "Script")
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000139// Note that 'NATIVE_' cases for functions and scripts are mapped onto
140// original tags when writing to the log.
141
ager@chromium.org357bf652010-04-12 11:30:10 +0000142
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000143class Sampler;
144
145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146class Logger {
147 public:
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000148#define DECLARE_ENUM(enum_item, ignore) enum_item,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000149 enum LogEventsAndTags {
150 LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM)
151 NUMBER_OF_LOG_EVENTS
152 };
153#undef DECLARE_ENUM
154
ager@chromium.org9085a012009-05-11 19:22:57 +0000155 // Acquires resources for logging if the right flags are set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000156 bool Setup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000158 void EnsureTickerStarted();
159 void EnsureTickerStopped();
160
161 Sampler* sampler();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000162
ager@chromium.org9085a012009-05-11 19:22:57 +0000163 // Frees resources acquired in Setup.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 void TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165
166 // Enable the computation of a sliding window of states.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000167 void EnableSlidingStateWindow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 // Emits an event with a string value -> (name, value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000170 void StringEvent(const char* name, const char* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171
172 // Emits an event with an int value -> (name, value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000173 void IntEvent(const char* name, int value);
174 void IntPtrTEvent(const char* name, intptr_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175
176 // Emits an event with an handle value -> (name, location).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000177 void HandleEvent(const char* name, Object** location);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178
179 // Emits memory management events for C allocated structures.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000180 void NewEvent(const char* name, void* object, size_t size);
181 void DeleteEvent(const char* name, void* object);
182
183 // Static versions of the above, operate on current isolate's logger.
184 // Used in TRACK_MEMORY(TypeName) defined in globals.h
185 static void NewEventStatic(const char* name, void* object, size_t size);
186 static void DeleteEventStatic(const char* name, void* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187
188 // Emits an event with a tag, and some resource usage information.
189 // -> (name, tag, <rusage information>).
190 // Currently, the resource usage information is a process time stamp
191 // and a real time timestamp.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000192 void ResourceEvent(const char* name, const char* tag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193
194 // Emits an event that an undefined property was read from an
195 // object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000196 void SuspectReadEvent(String* name, Object* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000198 // Emits an event when a message is put on or read from a debugging queue.
199 // DebugTag lets us put a call-site specific label on the event.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000200 void DebugTag(const char* call_site_tag);
201 void DebugEvent(const char* event_type, Vector<uint16_t> parameter);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000202
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203
204 // ==== Events logged by --log-api. ====
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000205 void ApiNamedSecurityCheck(Object* key);
206 void ApiIndexedSecurityCheck(uint32_t index);
207 void ApiNamedPropertyAccess(const char* tag, JSObject* holder, Object* name);
208 void ApiIndexedPropertyAccess(const char* tag,
209 JSObject* holder,
210 uint32_t index);
211 void ApiObjectAccess(const char* tag, JSObject* obj);
212 void ApiEntryCall(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213
214
215 // ==== Events logged by --log-code. ====
ager@chromium.org01beca72009-11-24 14:29:16 +0000216 // Emits a code event for a callback function.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000217 void CallbackEvent(String* name, Address entry_point);
218 void GetterCallbackEvent(String* name, Address entry_point);
219 void SetterCallbackEvent(String* name, Address entry_point);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220 // Emits a code create event.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000221 void CodeCreateEvent(LogEventsAndTags tag,
222 Code* code, const char* source);
223 void CodeCreateEvent(LogEventsAndTags tag,
224 Code* code, String* name);
225 void CodeCreateEvent(LogEventsAndTags tag,
226 Code* code,
227 SharedFunctionInfo* shared,
228 String* name);
229 void CodeCreateEvent(LogEventsAndTags tag,
230 Code* code,
231 SharedFunctionInfo* shared,
232 String* source, int line);
233 void CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count);
234 void CodeMovingGCEvent();
ager@chromium.org71daaf62009-04-01 07:22:49 +0000235 // Emits a code create event for a RegExp.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000236 void RegExpCodeCreateEvent(Code* code, String* source);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 // Emits a code move event.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000238 void CodeMoveEvent(Address from, Address to);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 // Emits a code delete event.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000240 void CodeDeleteEvent(Address from);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000241
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000242 void SharedFunctionInfoMoveEvent(Address from, Address to);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000243
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000244 void SnapshotPositionEvent(Address addr, int pos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245
246 // ==== Events logged by --log-gc. ====
247 // Heap sampling events: start, end, and individual types.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000248 void HeapSampleBeginEvent(const char* space, const char* kind);
249 void HeapSampleEndEvent(const char* space, const char* kind);
250 void HeapSampleItemEvent(const char* type, int number, int bytes);
251 void HeapSampleJSConstructorEvent(const char* constructor,
252 int number, int bytes);
253 void HeapSampleJSRetainersEvent(const char* constructor,
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000254 const char* event);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000255 void HeapSampleJSProducerEvent(const char* constructor,
256 Address* stack);
257 void HeapSampleStats(const char* space, const char* kind,
258 intptr_t capacity, intptr_t used);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000260 void SharedLibraryEvent(const char* library_path,
261 uintptr_t start,
262 uintptr_t end);
263 void SharedLibraryEvent(const wchar_t* library_path,
264 uintptr_t start,
265 uintptr_t end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000267 // ==== Events logged by --log-regexp ====
268 // Regexp compilation and execution events.
269
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000270 void RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000271
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000272 // Log an event reported from generated code
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000273 void LogRuntime(Vector<const char> format, JSArray* args);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000274
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275#ifdef ENABLE_LOGGING_AND_PROFILING
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000276 bool is_logging() {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000277 return logging_nesting_ > 0;
ager@chromium.org3e875802009-06-29 08:26:34 +0000278 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000279
iposva@chromium.org245aa852009-02-10 00:49:54 +0000280 // Pause/Resume collection of profiling data.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000281 // When data collection is paused, CPU Tick events are discarded until
iposva@chromium.org245aa852009-02-10 00:49:54 +0000282 // data collection is Resumed.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000283 void PauseProfiler();
284 void ResumeProfiler();
285 bool IsProfilerPaused();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000286
ager@chromium.org9085a012009-05-11 19:22:57 +0000287 // If logging is performed into a memory buffer, allows to
288 // retrieve previously written messages. See v8.h.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000289 int GetLogLines(int from_pos, char* dest_buf, int max_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000290
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000291 // Logs all compiled functions found in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000292 void LogCompiledFunctions();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000293 // Logs all accessor callbacks found in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000294 void LogAccessorCallbacks();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000295 // Used for logging stubs found in the snapshot.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000296 void LogCodeObjects();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000297
ager@chromium.org357bf652010-04-12 11:30:10 +0000298 // Converts tag to a corresponding NATIVE_... if the script is native.
299 INLINE(static LogEventsAndTags ToNativeByScript(LogEventsAndTags, Script*));
300
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000301 // Profiler's sampling interval (in milliseconds).
302 static const int kSamplingIntervalMs = 1;
303
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000304 // Callback from Log, stops profiling in case of insufficient resources.
305 void LogFailure();
306
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000307 private:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000308 class NameBuffer;
309 class NameMap;
310
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000311 Logger();
312 ~Logger();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000313
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000314 // Emits the profiler's first message.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000315 void ProfilerBeginEvent();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000316
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000317 // Emits callback event messages.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000318 void CallbackEventInternal(const char* prefix,
319 const char* name,
320 Address entry_point);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000321
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000322 // Internal configurable move event.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000323 void MoveEventInternal(LogEventsAndTags event, Address from, Address to);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000324
325 // Internal configurable move event.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000326 void DeleteEventInternal(LogEventsAndTags event, Address from);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000327
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000328 // Emits the source code of a regexp. Used by regexp events.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000329 void LogRegExpSource(Handle<JSRegExp> regexp);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000330
ager@chromium.org5c838252010-02-19 08:53:10 +0000331 // Used for logging stubs found in the snapshot.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000332 void LogCodeObject(Object* code_object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000333
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000334 // Emits general information about generated code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000335 void LogCodeInfo();
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000336
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000337 void RegisterSnapshotCodeName(Code* code, const char* name, int name_size);
338
339 // Low-level logging support.
340
341 void LowLevelCodeCreateEvent(Code* code, const char* name, int name_size);
342
343 void LowLevelCodeMoveEvent(Address from, Address to);
344
345 void LowLevelCodeDeleteEvent(Address from);
346
347 void LowLevelSnapshotPositionEvent(Address addr, int pos);
348
349 void LowLevelLogWriteBytes(const char* bytes, int size);
350
351 template <typename T>
352 void LowLevelLogWriteStruct(const T& s) {
353 char tag = T::kTag;
354 LowLevelLogWriteBytes(reinterpret_cast<const char*>(&tag), sizeof(tag));
355 LowLevelLogWriteBytes(reinterpret_cast<const char*>(&s), sizeof(s));
356 }
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000357
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358 // Emits a profiler tick event. Used by the profiler thread.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000359 void TickEvent(TickSample* sample, bool overflow);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000361 void ApiEvent(const char* name, ...);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362
ager@chromium.org6f10e412009-02-13 10:11:16 +0000363 // Logs a StringEvent regardless of whether FLAG_log is true.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000364 void UncheckedStringEvent(const char* name, const char* value);
ager@chromium.org6f10e412009-02-13 10:11:16 +0000365
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000366 // Logs an IntEvent regardless of whether FLAG_log is true.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000367 void UncheckedIntEvent(const char* name, int value);
368 void UncheckedIntPtrTEvent(const char* name, intptr_t value);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000369
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000370 // Returns whether profiler's sampler is active.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000371 bool IsProfilerSamplerActive();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000372
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 // The sampler used by the profiler and the sliding state window.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000374 Ticker* ticker_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375
376 // When the statistical profile is active, profiler_
377 // points to a Profiler, that handles collection
378 // of samples.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000379 Profiler* profiler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 // SlidingStateWindow instance keeping a sliding window of the most
382 // recent VM states.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000383 SlidingStateWindow* sliding_state_window_;
384
385 // An array of log events names.
386 const char* const* log_events_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387
388 // Internal implementation classes with access to
389 // private members.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 friend class EventLog;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000391 friend class Isolate;
392 friend class LogMessageBuilder;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 friend class TimeLog;
394 friend class Profiler;
395 friend class SlidingStateWindow;
ager@chromium.org01beca72009-11-24 14:29:16 +0000396 friend class StackTracer;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397 friend class VMState;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000398
399 friend class LoggerTestHelper;
ager@chromium.org3e875802009-06-29 08:26:34 +0000400
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000401
402 int logging_nesting_;
403 int cpu_profiler_nesting_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000404
405 Log* log_;
406
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000407 NameBuffer* name_buffer_;
408
409 NameMap* address_to_name_map_;
410
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000411 // Guards against multiple calls to TearDown() that can happen in some tests.
412 // 'true' between Setup() and TearDown().
413 bool is_initialized_;
414
415 // Support for 'incremental addresses' in compressed logs:
416 // LogMessageBuilder::AppendAddress(Address addr)
417 Address last_address_;
418 // Logger::TickEvent(...)
419 Address prev_sp_;
420 Address prev_function_;
421 // Logger::MoveEventInternal(...)
422 Address prev_to_;
423 // Logger::FunctionCreateEvent(...)
424 Address prev_code_;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000425
426 friend class CpuProfiler;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000427#else
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000428 bool is_logging() { return false; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429#endif
430};
431
432
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000433// Process wide registry of samplers.
434class SamplerRegistry : public AllStatic {
435 public:
436 enum State {
437 HAS_NO_SAMPLERS,
438 HAS_SAMPLERS,
439 HAS_CPU_PROFILING_SAMPLERS
440 };
441
442 typedef void (*VisitSampler)(Sampler*, void*);
443
444 static State GetState();
445
446 // Iterates over all active samplers keeping the internal lock held.
447 // Returns whether there are any active samplers.
448 static bool IterateActiveSamplers(VisitSampler func, void* param);
449
450 // Adds/Removes an active sampler.
451 static void AddActiveSampler(Sampler* sampler);
452 static void RemoveActiveSampler(Sampler* sampler);
453
454 private:
455 static bool ActiveSamplersExist() {
456 return active_samplers_ != NULL && !active_samplers_->is_empty();
457 }
458
459 static Mutex* mutex_; // Protects the state below.
460 static List<Sampler*>* active_samplers_;
461
462 DISALLOW_IMPLICIT_CONSTRUCTORS(SamplerRegistry);
463};
464
465
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000466// Class that extracts stack trace, used for profiling.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000467class StackTracer : public AllStatic {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000468 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000469 static void Trace(Isolate* isolate, TickSample* sample);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000470};
471
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472} } // namespace v8::internal
473
lrn@chromium.org25156de2010-04-06 13:10:27 +0000474
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475#endif // V8_LOG_H_