blob: 4d5acced692c9ba2701cedbd266b87f2f6bd98c5 [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
90
91class VMState BASE_EMBEDDED {
92#ifdef ENABLE_LOGGING_AND_PROFILING
93 public:
Steve Blockd0582a62009-12-15 09:54:21 +000094 inline VMState(StateTag state);
Steve Blocka7e24c12009-10-30 11:49:00 +000095 inline ~VMState();
96
97 StateTag state() { return state_; }
Steve Blockd0582a62009-12-15 09:54:21 +000098 Address external_callback() { return external_callback_; }
99 void set_external_callback(Address external_callback) {
100 external_callback_ = external_callback;
101 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000102
103 private:
104 bool disabled_;
105 StateTag state_;
106 VMState* previous_;
Steve Blockd0582a62009-12-15 09:54:21 +0000107 Address external_callback_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000108#else
109 public:
110 explicit VMState(StateTag state) {}
111#endif
112};
113
114
115#define LOG_EVENTS_AND_TAGS_LIST(V) \
116 V(CODE_CREATION_EVENT, "code-creation", "cc") \
117 V(CODE_MOVE_EVENT, "code-move", "cm") \
118 V(CODE_DELETE_EVENT, "code-delete", "cd") \
119 V(TICK_EVENT, "tick", "t") \
120 V(REPEAT_META_EVENT, "repeat", "r") \
121 V(BUILTIN_TAG, "Builtin", "bi") \
122 V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak", "cdb") \
123 V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn", "cdbsi") \
124 V(CALL_IC_TAG, "CallIC", "cic") \
125 V(CALL_INITIALIZE_TAG, "CallInitialize", "ci") \
126 V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic", "cmm") \
127 V(CALL_MISS_TAG, "CallMiss", "cm") \
128 V(CALL_NORMAL_TAG, "CallNormal", "cn") \
129 V(CALL_PRE_MONOMORPHIC_TAG, "CallPreMonomorphic", "cpm") \
Steve Blockd0582a62009-12-15 09:54:21 +0000130 V(CALLBACK_TAG, "Callback", "cb") \
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 V(EVAL_TAG, "Eval", "e") \
132 V(FUNCTION_TAG, "Function", "f") \
133 V(KEYED_LOAD_IC_TAG, "KeyedLoadIC", "klic") \
134 V(KEYED_STORE_IC_TAG, "KeyedStoreIC", "ksic") \
135 V(LAZY_COMPILE_TAG, "LazyCompile", "lc") \
136 V(LOAD_IC_TAG, "LoadIC", "lic") \
137 V(REG_EXP_TAG, "RegExp", "re") \
138 V(SCRIPT_TAG, "Script", "sc") \
139 V(STORE_IC_TAG, "StoreIC", "sic") \
140 V(STUB_TAG, "Stub", "s")
141
142class Logger {
143 public:
144#define DECLARE_ENUM(enum_item, ignore1, ignore2) enum_item,
145 enum LogEventsAndTags {
146 LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM)
147 NUMBER_OF_LOG_EVENTS
148 };
149#undef DECLARE_ENUM
150
151 // Acquires resources for logging if the right flags are set.
152 static bool Setup();
153
154 // Frees resources acquired in Setup.
155 static void TearDown();
156
157 // Enable the computation of a sliding window of states.
158 static void EnableSlidingStateWindow();
159
160 // Write a raw string to the log to be used as a preamble.
161 // No check is made that the 'preamble' is actually at the beginning
162 // of the log. The preample is used to write code events saved in the
163 // snapshot.
164 static void Preamble(const char* content);
165
166 // Emits an event with a string value -> (name, value).
167 static void StringEvent(const char* name, const char* value);
168
169 // Emits an event with an int value -> (name, value).
170 static void IntEvent(const char* name, int value);
171
172 // Emits an event with an handle value -> (name, location).
173 static void HandleEvent(const char* name, Object** location);
174
175 // Emits memory management events for C allocated structures.
176 static void NewEvent(const char* name, void* object, size_t size);
177 static void DeleteEvent(const char* name, void* object);
178
179 // Emits an event with a tag, and some resource usage information.
180 // -> (name, tag, <rusage information>).
181 // Currently, the resource usage information is a process time stamp
182 // and a real time timestamp.
183 static void ResourceEvent(const char* name, const char* tag);
184
185 // Emits an event that an undefined property was read from an
186 // object.
187 static void SuspectReadEvent(String* name, Object* obj);
188
189 // Emits an event when a message is put on or read from a debugging queue.
190 // DebugTag lets us put a call-site specific label on the event.
191 static void DebugTag(const char* call_site_tag);
192 static void DebugEvent(const char* event_type, Vector<uint16_t> parameter);
193
194
195 // ==== Events logged by --log-api. ====
196 static void ApiNamedSecurityCheck(Object* key);
197 static void ApiIndexedSecurityCheck(uint32_t index);
198 static void ApiNamedPropertyAccess(const char* tag,
199 JSObject* holder,
200 Object* name);
201 static void ApiIndexedPropertyAccess(const char* tag,
202 JSObject* holder,
203 uint32_t index);
204 static void ApiObjectAccess(const char* tag, JSObject* obj);
205 static void ApiEntryCall(const char* name);
206
207
208 // ==== Events logged by --log-code. ====
Steve Blockd0582a62009-12-15 09:54:21 +0000209 // Emits a code event for a callback function.
210 static void CallbackEvent(String* name, Address entry_point);
211 static void GetterCallbackEvent(String* name, Address entry_point);
212 static void SetterCallbackEvent(String* name, Address entry_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 // Emits a code create event.
214 static void CodeCreateEvent(LogEventsAndTags tag,
215 Code* code, const char* source);
216 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, String* name);
217 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, String* name,
218 String* source, int line);
219 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count);
220 // Emits a code create event for a RegExp.
221 static void RegExpCodeCreateEvent(Code* code, String* source);
222 // Emits a code move event.
223 static void CodeMoveEvent(Address from, Address to);
224 // Emits a code delete event.
225 static void CodeDeleteEvent(Address from);
226
227 // ==== Events logged by --log-gc. ====
228 // Heap sampling events: start, end, and individual types.
229 static void HeapSampleBeginEvent(const char* space, const char* kind);
230 static void HeapSampleEndEvent(const char* space, const char* kind);
231 static void HeapSampleItemEvent(const char* type, int number, int bytes);
232 static void HeapSampleJSConstructorEvent(const char* constructor,
233 int number, int bytes);
234 static void HeapSampleJSRetainersEvent(const char* constructor,
235 const char* event);
Steve Block3ce2e202009-11-05 08:53:23 +0000236 static void HeapSampleJSProducerEvent(const char* constructor,
237 Address* stack);
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 static void HeapSampleStats(const char* space, const char* kind,
239 int capacity, int used);
240
241 static void SharedLibraryEvent(const char* library_path,
242 uintptr_t start,
243 uintptr_t end);
244 static void SharedLibraryEvent(const wchar_t* library_path,
245 uintptr_t start,
246 uintptr_t end);
247
248 // ==== Events logged by --log-regexp ====
249 // Regexp compilation and execution events.
250
251 static void RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache);
252
253 // Log an event reported from generated code
254 static void LogRuntime(Vector<const char> format, JSArray* args);
255
256#ifdef ENABLE_LOGGING_AND_PROFILING
257 static StateTag state() {
258 return current_state_ ? current_state_->state() : OTHER;
259 }
260
261 static bool is_logging() {
262 return is_logging_;
263 }
264
265 // Pause/Resume collection of profiling data.
266 // When data collection is paused, CPU Tick events are discarded until
267 // data collection is Resumed.
268 static void PauseProfiler(int flags);
269 static void ResumeProfiler(int flags);
270 static int GetActiveProfilerModules();
271
272 // If logging is performed into a memory buffer, allows to
273 // retrieve previously written messages. See v8.h.
274 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
275
276 // Logs all compiled functions found in the heap.
277 static void LogCompiledFunctions();
Steve Blockd0582a62009-12-15 09:54:21 +0000278 // Logs all accessor callbacks found in the heap.
279 static void LogAccessorCallbacks();
280 // Used for logging stubs found in the snapshot.
281 static void LogCodeObject(Object* code_object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000282
283 private:
284
285 // Profiler's sampling interval (in milliseconds).
286 static const int kSamplingIntervalMs = 1;
287
288 // Size of window used for log records compression.
289 static const int kCompressionWindowSize = 4;
290
291 // Emits the profiler's first message.
292 static void ProfilerBeginEvent();
293
Steve Blockd0582a62009-12-15 09:54:21 +0000294 // Emits callback event messages.
295 static void CallbackEventInternal(const char* prefix,
296 const char* name,
297 Address entry_point);
298
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 // Emits aliases for compressed messages.
300 static void LogAliases();
301
302 // Emits the source code of a regexp. Used by regexp events.
303 static void LogRegExpSource(Handle<JSRegExp> regexp);
304
305 // 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
313 // Stops logging and profiling in case of insufficient resources.
314 static void StopLoggingAndProfiling();
315
316 // Returns whether profiler's sampler is active.
317 static bool IsProfilerSamplerActive();
318
319 // The sampler used by the profiler and the sliding state window.
320 static Ticker* ticker_;
321
322 // When the statistical profile is active, profiler_
323 // points to a Profiler, that handles collection
324 // of samples.
325 static Profiler* profiler_;
326
327 // A stack of VM states.
328 static VMState* current_state_;
329
330 // Singleton bottom or default vm state.
331 static VMState bottom_state_;
332
333 // SlidingStateWindow instance keeping a sliding window of the most
334 // recent VM states.
335 static SlidingStateWindow* sliding_state_window_;
336
337 // An array of log events names.
338 static const char** log_events_;
339
340 // An instance of helper created if log compression is enabled.
341 static CompressionHelper* compression_helper_;
342
343 // Internal implementation classes with access to
344 // private members.
345 friend class CompressionHelper;
346 friend class EventLog;
347 friend class TimeLog;
348 friend class Profiler;
349 friend class SlidingStateWindow;
Steve Blockd0582a62009-12-15 09:54:21 +0000350 friend class StackTracer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 friend class VMState;
352
353 friend class LoggerTestHelper;
354
355 static bool is_logging_;
356#else
357 static bool is_logging() { return false; }
358#endif
359};
360
361
362// Class that extracts stack trace, used for profiling.
363class StackTracer : public AllStatic {
364 public:
365 static void Trace(TickSample* sample);
366};
367
368
369} } // namespace v8::internal
370
371#endif // V8_LOG_H_