log.h revision b0fe1620dcb4135ac3ab2d66ff93072373911299
1// 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; 77 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 89#define LOG_EVENTS_AND_TAGS_LIST(V) \ 90 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") \ 94 V(FUNCTION_CREATION_EVENT, "function-creation") \ 95 V(FUNCTION_MOVE_EVENT, "function-move") \ 96 V(FUNCTION_DELETE_EVENT, "function-delete") \ 97 V(SNAPSHOT_POSITION_EVENT, "snapshot-pos") \ 98 V(TICK_EVENT, "tick") \ 99 V(REPEAT_META_EVENT, "repeat") \ 100 V(BUILTIN_TAG, "Builtin") \ 101 V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak") \ 102 V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn") \ 103 V(CALL_IC_TAG, "CallIC") \ 104 V(CALL_INITIALIZE_TAG, "CallInitialize") \ 105 V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic") \ 106 V(CALL_MISS_TAG, "CallMiss") \ 107 V(CALL_NORMAL_TAG, "CallNormal") \ 108 V(CALL_PRE_MONOMORPHIC_TAG, "CallPreMonomorphic") \ 109 V(KEYED_CALL_DEBUG_BREAK_TAG, "KeyedCallDebugBreak") \ 110 V(KEYED_CALL_DEBUG_PREPARE_STEP_IN_TAG, \ 111 "KeyedCallDebugPrepareStepIn") \ 112 V(KEYED_CALL_IC_TAG, "KeyedCallIC") \ 113 V(KEYED_CALL_INITIALIZE_TAG, "KeyedCallInitialize") \ 114 V(KEYED_CALL_MEGAMORPHIC_TAG, "KeyedCallMegamorphic") \ 115 V(KEYED_CALL_MISS_TAG, "KeyedCallMiss") \ 116 V(KEYED_CALL_NORMAL_TAG, "KeyedCallNormal") \ 117 V(KEYED_CALL_PRE_MONOMORPHIC_TAG, "KeyedCallPreMonomorphic") \ 118 V(CALLBACK_TAG, "Callback") \ 119 V(EVAL_TAG, "Eval") \ 120 V(FUNCTION_TAG, "Function") \ 121 V(KEYED_LOAD_IC_TAG, "KeyedLoadIC") \ 122 V(KEYED_STORE_IC_TAG, "KeyedStoreIC") \ 123 V(LAZY_COMPILE_TAG, "LazyCompile") \ 124 V(LOAD_IC_TAG, "LoadIC") \ 125 V(REG_EXP_TAG, "RegExp") \ 126 V(SCRIPT_TAG, "Script") \ 127 V(STORE_IC_TAG, "StoreIC") \ 128 V(STUB_TAG, "Stub") \ 129 V(NATIVE_FUNCTION_TAG, "Function") \ 130 V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile") \ 131 V(NATIVE_SCRIPT_TAG, "Script") 132// Note that 'NATIVE_' cases for functions and scripts are mapped onto 133// original tags when writing to the log. 134 135 136class Logger { 137 public: 138#define DECLARE_ENUM(enum_item, ignore) enum_item, 139 enum LogEventsAndTags { 140 LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM) 141 NUMBER_OF_LOG_EVENTS 142 }; 143#undef DECLARE_ENUM 144 145 // Acquires resources for logging if the right flags are set. 146 static bool Setup(); 147 148 static void EnsureTickerStarted(); 149 static void EnsureTickerStopped(); 150 151 // Frees resources acquired in Setup. 152 static void TearDown(); 153 154 // Enable the computation of a sliding window of states. 155 static void EnableSlidingStateWindow(); 156 157 // Emits an event with a string value -> (name, value). 158 static void StringEvent(const char* name, const char* value); 159 160 // Emits an event with an int value -> (name, value). 161 static void IntEvent(const char* name, int value); 162 static void IntPtrTEvent(const char* name, intptr_t value); 163 164 // Emits an event with an handle value -> (name, location). 165 static void HandleEvent(const char* name, Object** location); 166 167 // Emits memory management events for C allocated structures. 168 static void NewEvent(const char* name, void* object, size_t size); 169 static void DeleteEvent(const char* name, void* object); 170 171 // Emits an event with a tag, and some resource usage information. 172 // -> (name, tag, <rusage information>). 173 // Currently, the resource usage information is a process time stamp 174 // and a real time timestamp. 175 static void ResourceEvent(const char* name, const char* tag); 176 177 // Emits an event that an undefined property was read from an 178 // object. 179 static void SuspectReadEvent(String* name, Object* obj); 180 181 // Emits an event when a message is put on or read from a debugging queue. 182 // DebugTag lets us put a call-site specific label on the event. 183 static void DebugTag(const char* call_site_tag); 184 static void DebugEvent(const char* event_type, Vector<uint16_t> parameter); 185 186 187 // ==== Events logged by --log-api. ==== 188 static void ApiNamedSecurityCheck(Object* key); 189 static void ApiIndexedSecurityCheck(uint32_t index); 190 static void ApiNamedPropertyAccess(const char* tag, 191 JSObject* holder, 192 Object* name); 193 static void ApiIndexedPropertyAccess(const char* tag, 194 JSObject* holder, 195 uint32_t index); 196 static void ApiObjectAccess(const char* tag, JSObject* obj); 197 static void ApiEntryCall(const char* name); 198 199 200 // ==== Events logged by --log-code. ==== 201 // Emits a code event for a callback function. 202 static void CallbackEvent(String* name, Address entry_point); 203 static void GetterCallbackEvent(String* name, Address entry_point); 204 static void SetterCallbackEvent(String* name, Address entry_point); 205 // Emits a code create event. 206 static void CodeCreateEvent(LogEventsAndTags tag, 207 Code* code, const char* source); 208 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, String* name); 209 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, String* name, 210 String* source, int line); 211 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count); 212 static void CodeMovingGCEvent(); 213 // Emits a code create event for a RegExp. 214 static void RegExpCodeCreateEvent(Code* code, String* source); 215 // Emits a code move event. 216 static void CodeMoveEvent(Address from, Address to); 217 // Emits a code delete event. 218 static void CodeDeleteEvent(Address from); 219 // Emits a function object create event. 220 static void FunctionCreateEvent(JSFunction* function); 221 static void FunctionCreateEventFromMove(JSFunction* function); 222 // Emits a function move event. 223 static void FunctionMoveEvent(Address from, Address to); 224 // Emits a function delete event. 225 static void FunctionDeleteEvent(Address from); 226 227 static void SnapshotPositionEvent(Address addr, int pos); 228 229 // ==== 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); 238 static void HeapSampleJSProducerEvent(const char* constructor, 239 Address* stack); 240 static void HeapSampleStats(const char* space, const char* kind, 241 intptr_t capacity, intptr_t used); 242 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 259 static bool is_logging() { 260 return logging_nesting_ > 0; 261 } 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. 266 static void PauseProfiler(int flags, int tag); 267 static void ResumeProfiler(int flags, int tag); 268 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(); 276 // Logs all compiled JSFunction objects found in the heap. 277 static void LogFunctionObjects(); 278 // Logs all accessor callbacks found in the heap. 279 static void LogAccessorCallbacks(); 280 // Used for logging stubs found in the snapshot. 281 static void LogCodeObjects(); 282 283 // Converts tag to a corresponding NATIVE_... if the script is native. 284 INLINE(static LogEventsAndTags ToNativeByScript(LogEventsAndTags, Script*)); 285 286 // Profiler's sampling interval (in milliseconds). 287 static const int kSamplingIntervalMs = 1; 288 289 private: 290 291 // Emits the profiler's first message. 292 static void ProfilerBeginEvent(); 293 294 // Emits callback event messages. 295 static void CallbackEventInternal(const char* prefix, 296 const char* name, 297 Address entry_point); 298 299 // Internal configurable move event. 300 static void MoveEventInternal(LogEventsAndTags event, 301 Address from, 302 Address to); 303 304 // Internal configurable move event. 305 static void DeleteEventInternal(LogEventsAndTags event, 306 Address from); 307 308 // Emits the source code of a regexp. Used by regexp events. 309 static void LogRegExpSource(Handle<JSRegExp> regexp); 310 311 // Used for logging stubs found in the snapshot. 312 static void LogCodeObject(Object* code_object); 313 314 // Emits general information about generated code. 315 static void LogCodeInfo(); 316 317 // Handles code creation when low-level profiling is active. 318 static void LowLevelCodeCreateEvent(Code* code, LogMessageBuilder* msg); 319 320 // Emits a profiler tick event. Used by the profiler thread. 321 static void TickEvent(TickSample* sample, bool overflow); 322 323 static void ApiEvent(const char* name, ...); 324 325 // Logs a StringEvent regardless of whether FLAG_log is true. 326 static void UncheckedStringEvent(const char* name, const char* value); 327 328 // Logs an IntEvent regardless of whether FLAG_log is true. 329 static void UncheckedIntEvent(const char* name, int value); 330 static void UncheckedIntPtrTEvent(const char* name, intptr_t value); 331 332 // Stops logging and profiling in case of insufficient resources. 333 static void StopLoggingAndProfiling(); 334 335 // Returns whether profiler's sampler is active. 336 static bool IsProfilerSamplerActive(); 337 338 // The sampler used by the profiler and the sliding state window. 339 static Ticker* ticker_; 340 341 // When the statistical profile is active, profiler_ 342 // points to a Profiler, that handles collection 343 // of samples. 344 static Profiler* profiler_; 345 346 // SlidingStateWindow instance keeping a sliding window of the most 347 // recent VM states. 348 static SlidingStateWindow* sliding_state_window_; 349 350 // Internal implementation classes with access to 351 // private members. 352 friend class EventLog; 353 friend class TimeLog; 354 friend class Profiler; 355 friend class SlidingStateWindow; 356 friend class StackTracer; 357 friend class VMState; 358 359 friend class LoggerTestHelper; 360 361 static int logging_nesting_; 362 static int cpu_profiler_nesting_; 363 static int heap_profiler_nesting_; 364 365 friend class CpuProfiler; 366#else 367 static bool is_logging() { return false; } 368#endif 369}; 370 371 372// Class that extracts stack trace, used for profiling. 373class StackTracer : public AllStatic { 374 public: 375 static void Trace(TickSample* sample); 376}; 377 378} } // namespace v8::internal 379 380 381#endif // V8_LOG_H_ 382