ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | namespace v8 { namespace internal { |
| 32 | |
| 33 | // Logger is used for collecting logging information from V8 during |
| 34 | // execution. The result is dumped to a file. |
| 35 | // |
| 36 | // Available command line flags: |
| 37 | // |
| 38 | // --log |
| 39 | // Minimal logging (no API, code, or GC sample events), default is off. |
| 40 | // |
| 41 | // --log-all |
| 42 | // Log all events to the file, default is off. This is the same as combining |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 43 | // --log-api, --log-code, --log-gc, and --log-regexp. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | // |
| 45 | // --log-api |
| 46 | // Log API events to the logfile, default is off. --log-api implies --log. |
| 47 | // |
| 48 | // --log-code |
| 49 | // Log code (create, move, and delete) events to the logfile, default is off. |
| 50 | // --log-code implies --log. |
| 51 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | // --log-gc |
| 53 | // Log GC heap samples after each GC that can be processed by hp2ps, default |
| 54 | // is off. --log-gc implies --log. |
| 55 | // |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 56 | // --log-regexp |
| 57 | // Log creation and use of regular expressions, Default is off. |
| 58 | // --log-regexp implies --log. |
| 59 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | // --logfile <filename> |
| 61 | // Specify the name of the logfile, default is "v8.log". |
| 62 | // |
| 63 | // --prof |
| 64 | // Collect statistical profiling information (ticks), default is off. The |
| 65 | // tick profiler requires code events, so --prof implies --log-code. |
| 66 | |
| 67 | // Forward declarations. |
| 68 | class Ticker; |
| 69 | class Profiler; |
| 70 | class Semaphore; |
| 71 | class SlidingStateWindow; |
| 72 | |
| 73 | #undef LOG |
| 74 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 75 | #define LOG(Call) v8::internal::Logger::Call |
| 76 | #else |
| 77 | #define LOG(Call) ((void) 0) |
| 78 | #endif |
| 79 | |
| 80 | |
| 81 | class VMState { |
| 82 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 83 | public: |
| 84 | explicit VMState(StateTag state); |
| 85 | ~VMState(); |
| 86 | |
| 87 | StateTag state() { return state_; } |
| 88 | |
| 89 | private: |
| 90 | StateTag state_; |
| 91 | VMState* previous_; |
| 92 | #else |
| 93 | public: |
| 94 | explicit VMState(StateTag state) {} |
| 95 | #endif |
| 96 | }; |
| 97 | |
| 98 | |
| 99 | class Logger { |
| 100 | public: |
| 101 | // Opens the file for logging if the right flags are set. |
| 102 | static bool Setup(); |
| 103 | |
| 104 | // Closes file opened in Setup. |
| 105 | static void TearDown(); |
| 106 | |
| 107 | // Enable the computation of a sliding window of states. |
| 108 | static void EnableSlidingStateWindow(); |
| 109 | |
| 110 | // Write a raw string to the log to be used as a preamble. |
| 111 | // No check is made that the 'preamble' is actually at the beginning |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 112 | // of the log. The preample is used to write code events saved in the |
| 113 | // snapshot. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | static void Preamble(const char* content); |
| 115 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | // Emits an event with a string value -> (name, value). |
| 117 | static void StringEvent(const char* name, const char* value); |
| 118 | |
| 119 | // Emits an event with an int value -> (name, value). |
| 120 | static void IntEvent(const char* name, int value); |
| 121 | |
| 122 | // Emits an event with an handle value -> (name, location). |
| 123 | static void HandleEvent(const char* name, Object** location); |
| 124 | |
| 125 | // Emits memory management events for C allocated structures. |
| 126 | static void NewEvent(const char* name, void* object, size_t size); |
| 127 | static void DeleteEvent(const char* name, void* object); |
| 128 | |
| 129 | // Emits an event with a tag, and some resource usage information. |
| 130 | // -> (name, tag, <rusage information>). |
| 131 | // Currently, the resource usage information is a process time stamp |
| 132 | // and a real time timestamp. |
| 133 | static void ResourceEvent(const char* name, const char* tag); |
| 134 | |
| 135 | // Emits an event that an undefined property was read from an |
| 136 | // object. |
| 137 | static void SuspectReadEvent(String* name, String* obj); |
| 138 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 139 | // Emits an event when a message is put on or read from a debugging queue. |
| 140 | // DebugTag lets us put a call-site specific label on the event. |
| 141 | static void DebugTag(const char* call_site_tag); |
| 142 | static void DebugEvent(const char* event_type, Vector<uint16_t> parameter); |
| 143 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | |
| 145 | // ==== Events logged by --log-api. ==== |
| 146 | static void ApiNamedSecurityCheck(Object* key); |
| 147 | static void ApiIndexedSecurityCheck(uint32_t index); |
| 148 | static void ApiNamedPropertyAccess(const char* tag, |
| 149 | JSObject* holder, |
| 150 | Object* name); |
| 151 | static void ApiIndexedPropertyAccess(const char* tag, |
| 152 | JSObject* holder, |
| 153 | uint32_t index); |
| 154 | static void ApiObjectAccess(const char* tag, JSObject* obj); |
| 155 | static void ApiEntryCall(const char* name); |
| 156 | |
| 157 | |
| 158 | // ==== Events logged by --log-code. ==== |
| 159 | // Emits a code create event. |
| 160 | static void CodeCreateEvent(const char* tag, Code* code, const char* source); |
| 161 | static void CodeCreateEvent(const char* tag, Code* code, String* name); |
| 162 | static void CodeCreateEvent(const char* tag, Code* code, int args_count); |
| 163 | // Emits a code move event. |
| 164 | static void CodeMoveEvent(Address from, Address to); |
| 165 | // Emits a code delete event. |
| 166 | static void CodeDeleteEvent(Address from); |
| 167 | |
| 168 | // ==== Events logged by --log-gc. ==== |
| 169 | // Heap sampling events: start, end, and individual types. |
| 170 | static void HeapSampleBeginEvent(const char* space, const char* kind); |
| 171 | static void HeapSampleEndEvent(const char* space, const char* kind); |
| 172 | static void HeapSampleItemEvent(const char* type, int number, int bytes); |
| 173 | |
| 174 | static void SharedLibraryEvent(const char* library_path, |
| 175 | unsigned start, |
| 176 | unsigned end); |
| 177 | static void SharedLibraryEvent(const wchar_t* library_path, |
| 178 | unsigned start, |
| 179 | unsigned end); |
| 180 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 181 | // ==== Events logged by --log-regexp ==== |
| 182 | // Regexp compilation and execution events. |
| 183 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 184 | static void RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 185 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 186 | static void RegExpExecEvent(Handle<JSRegExp> regexp, |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 187 | int start_index, |
| 188 | Handle<String> input_string); |
| 189 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 190 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 191 | static StateTag state() { |
| 192 | return current_state_ ? current_state_->state() : OTHER; |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 197 | private: |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 198 | |
| 199 | // Emits the source code of a regexp. Used by regexp events. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 200 | static void LogRegExpSource(Handle<JSRegExp> regexp); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 201 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 202 | static void LogString(Handle<String> str); |
| 203 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | // Emits a profiler tick event. Used by the profiler thread. |
| 205 | static void TickEvent(TickSample* sample, bool overflow); |
| 206 | |
| 207 | static void ApiEvent(const char* name, ...); |
| 208 | |
| 209 | // When logging is active, logfile_ refers the file |
| 210 | // events are written to. |
| 211 | static FILE* logfile_; |
| 212 | |
| 213 | // The sampler used by the profiler and the sliding state window. |
| 214 | static Ticker* ticker_; |
| 215 | |
| 216 | // When the statistical profile is active, profiler_ |
| 217 | // points to a Profiler, that handles collection |
| 218 | // of samples. |
| 219 | static Profiler* profiler_; |
| 220 | |
| 221 | // mutex_ is a Mutex used for enforcing exclusive |
| 222 | // access to the log file. |
| 223 | static Mutex* mutex_; |
| 224 | |
| 225 | // A stack of VM states. |
| 226 | static VMState* current_state_; |
| 227 | |
| 228 | // SlidingStateWindow instance keeping a sliding window of the most |
| 229 | // recent VM states. |
| 230 | static SlidingStateWindow* sliding_state_window_; |
| 231 | |
| 232 | // Internal implementation classes with access to |
| 233 | // private members. |
| 234 | friend class EventLog; |
| 235 | friend class TimeLog; |
| 236 | friend class Profiler; |
| 237 | friend class SlidingStateWindow; |
| 238 | friend class VMState; |
| 239 | #endif |
| 240 | }; |
| 241 | |
| 242 | |
| 243 | } } // namespace v8::internal |
| 244 | |
| 245 | #endif // V8_LOG_H_ |