ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1 | // Copyright 2009 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 | #include <stdarg.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "log.h" |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 34 | #include "log-utils.h" |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 35 | #include "macro-assembler.h" |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 36 | #include "platform.h" |
| 37 | #include "serialize.h" |
| 38 | #include "string-stream.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
| 43 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 44 | |
| 45 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | // Sliding state window. Updates counters to keep track of the last |
| 47 | // window of kBufferSize states. This is useful to track where we |
| 48 | // spent our time. |
| 49 | // |
| 50 | class SlidingStateWindow { |
| 51 | public: |
| 52 | SlidingStateWindow(); |
| 53 | ~SlidingStateWindow(); |
| 54 | void AddState(StateTag state); |
| 55 | |
| 56 | private: |
| 57 | static const int kBufferSize = 256; |
| 58 | int current_index_; |
| 59 | bool is_full_; |
| 60 | byte buffer_[kBufferSize]; |
| 61 | |
| 62 | |
| 63 | void IncrementStateCounter(StateTag state) { |
| 64 | Counters::state_counters[state].Increment(); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | void DecrementStateCounter(StateTag state) { |
| 69 | Counters::state_counters[state].Decrement(); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | // |
| 75 | // The Profiler samples pc and sp values for the main thread. |
| 76 | // Each sample is appended to a circular buffer. |
| 77 | // An independent thread removes data and writes it to the log. |
| 78 | // This design minimizes the time spent in the sampler. |
| 79 | // |
| 80 | class Profiler: public Thread { |
| 81 | public: |
| 82 | Profiler(); |
| 83 | void Engage(); |
| 84 | void Disengage(); |
| 85 | |
| 86 | // Inserts collected profiling data into buffer. |
| 87 | void Insert(TickSample* sample) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 88 | if (paused_) |
| 89 | return; |
| 90 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | if (Succ(head_) == tail_) { |
| 92 | overflow_ = true; |
| 93 | } else { |
| 94 | buffer_[head_] = *sample; |
| 95 | head_ = Succ(head_); |
| 96 | buffer_semaphore_->Signal(); // Tell we have an element. |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Waits for a signal and removes profiling data. |
| 101 | bool Remove(TickSample* sample) { |
| 102 | buffer_semaphore_->Wait(); // Wait for an element. |
| 103 | *sample = buffer_[tail_]; |
| 104 | bool result = overflow_; |
| 105 | tail_ = Succ(tail_); |
| 106 | overflow_ = false; |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | void Run(); |
| 111 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 112 | // Pause and Resume TickSample data collection. |
| 113 | static bool paused() { return paused_; } |
| 114 | static void pause() { paused_ = true; } |
| 115 | static void resume() { paused_ = false; } |
| 116 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | private: |
| 118 | // Returns the next index in the cyclic buffer. |
| 119 | int Succ(int index) { return (index + 1) % kBufferSize; } |
| 120 | |
| 121 | // Cyclic buffer for communicating profiling samples |
| 122 | // between the signal handler and the worker thread. |
| 123 | static const int kBufferSize = 128; |
| 124 | TickSample buffer_[kBufferSize]; // Buffer storage. |
| 125 | int head_; // Index to the buffer head. |
| 126 | int tail_; // Index to the buffer tail. |
| 127 | bool overflow_; // Tell whether a buffer overflow has occurred. |
| 128 | Semaphore* buffer_semaphore_; // Sempahore used for buffer synchronization. |
| 129 | |
| 130 | // Tells whether worker thread should continue running. |
| 131 | bool running_; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 132 | |
| 133 | // Tells whether we are currently recording tick samples. |
| 134 | static bool paused_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 137 | bool Profiler::paused_ = false; |
| 138 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 139 | |
| 140 | // |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 141 | // StackTracer implementation |
| 142 | // |
| 143 | void StackTracer::Trace(TickSample* sample) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 144 | if (sample->state == GC) { |
| 145 | sample->frames_count = 0; |
| 146 | return; |
| 147 | } |
| 148 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 149 | const Address js_entry_sp = Top::js_entry_sp(Top::GetCurrentThread()); |
| 150 | if (js_entry_sp == 0) { |
| 151 | // Not executing JS now. |
| 152 | sample->frames_count = 0; |
| 153 | return; |
| 154 | } |
| 155 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 156 | SafeStackTraceFrameIterator it( |
| 157 | reinterpret_cast<Address>(sample->fp), |
| 158 | reinterpret_cast<Address>(sample->sp), |
| 159 | reinterpret_cast<Address>(sample->sp), |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 160 | js_entry_sp); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 161 | int i = 0; |
| 162 | while (!it.done() && i < TickSample::kMaxFramesCount) { |
| 163 | sample->stack[i++] = it.frame()->pc(); |
| 164 | it.Advance(); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 165 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 166 | sample->frames_count = i; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
| 170 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 171 | // Ticker used to provide ticks to the profiler and the sliding state |
| 172 | // window. |
| 173 | // |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 174 | class Ticker: public Sampler { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | public: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 176 | explicit Ticker(int interval): |
| 177 | Sampler(interval, FLAG_prof), window_(NULL), profiler_(NULL) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 178 | |
| 179 | ~Ticker() { if (IsActive()) Stop(); } |
| 180 | |
| 181 | void Tick(TickSample* sample) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 182 | if (IsProfiling()) StackTracer::Trace(sample); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 183 | if (profiler_) profiler_->Insert(sample); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 184 | if (window_) window_->AddState(sample->state); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void SetWindow(SlidingStateWindow* window) { |
| 188 | window_ = window; |
| 189 | if (!IsActive()) Start(); |
| 190 | } |
| 191 | |
| 192 | void ClearWindow() { |
| 193 | window_ = NULL; |
| 194 | if (!profiler_ && IsActive()) Stop(); |
| 195 | } |
| 196 | |
| 197 | void SetProfiler(Profiler* profiler) { |
| 198 | profiler_ = profiler; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 199 | if (!FLAG_prof_lazy && !IsActive()) Start(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void ClearProfiler() { |
| 203 | profiler_ = NULL; |
| 204 | if (!window_ && IsActive()) Stop(); |
| 205 | } |
| 206 | |
| 207 | private: |
| 208 | SlidingStateWindow* window_; |
| 209 | Profiler* profiler_; |
| 210 | }; |
| 211 | |
| 212 | |
| 213 | // |
| 214 | // SlidingStateWindow implementation. |
| 215 | // |
| 216 | SlidingStateWindow::SlidingStateWindow(): current_index_(0), is_full_(false) { |
| 217 | for (int i = 0; i < kBufferSize; i++) { |
| 218 | buffer_[i] = static_cast<byte>(OTHER); |
| 219 | } |
| 220 | Logger::ticker_->SetWindow(this); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | SlidingStateWindow::~SlidingStateWindow() { |
| 225 | Logger::ticker_->ClearWindow(); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | void SlidingStateWindow::AddState(StateTag state) { |
| 230 | if (is_full_) { |
| 231 | DecrementStateCounter(static_cast<StateTag>(buffer_[current_index_])); |
| 232 | } else if (current_index_ == kBufferSize - 1) { |
| 233 | is_full_ = true; |
| 234 | } |
| 235 | buffer_[current_index_] = static_cast<byte>(state); |
| 236 | IncrementStateCounter(state); |
| 237 | ASSERT(IsPowerOf2(kBufferSize)); |
| 238 | current_index_ = (current_index_ + 1) & (kBufferSize - 1); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | // |
| 243 | // Profiler implementation. |
| 244 | // |
| 245 | Profiler::Profiler() { |
| 246 | buffer_semaphore_ = OS::CreateSemaphore(0); |
| 247 | head_ = 0; |
| 248 | tail_ = 0; |
| 249 | overflow_ = false; |
| 250 | running_ = false; |
| 251 | } |
| 252 | |
| 253 | |
| 254 | void Profiler::Engage() { |
| 255 | OS::LogSharedLibraryAddresses(); |
| 256 | |
| 257 | // Start thread processing the profiler buffer. |
| 258 | running_ = true; |
| 259 | Start(); |
| 260 | |
| 261 | // Register to get ticks. |
| 262 | Logger::ticker_->SetProfiler(this); |
| 263 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 264 | Logger::ProfilerBeginEvent(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
| 268 | void Profiler::Disengage() { |
| 269 | // Stop receiving ticks. |
| 270 | Logger::ticker_->ClearProfiler(); |
| 271 | |
| 272 | // Terminate the worker thread by setting running_ to false, |
| 273 | // inserting a fake element in the queue and then wait for |
| 274 | // the thread to terminate. |
| 275 | running_ = false; |
| 276 | TickSample sample; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 277 | // Reset 'paused_' flag, otherwise semaphore may not be signalled. |
| 278 | resume(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | Insert(&sample); |
| 280 | Join(); |
| 281 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 282 | LOG(UncheckedStringEvent("profiler", "end")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
| 286 | void Profiler::Run() { |
| 287 | TickSample sample; |
| 288 | bool overflow = Logger::profiler_->Remove(&sample); |
| 289 | while (running_) { |
| 290 | LOG(TickEvent(&sample, overflow)); |
| 291 | overflow = Logger::profiler_->Remove(&sample); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 297 | // Logger class implementation. |
| 298 | // |
| 299 | Ticker* Logger::ticker_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | Profiler* Logger::profiler_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | VMState* Logger::current_state_ = NULL; |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 302 | VMState Logger::bottom_state_(EXTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 303 | SlidingStateWindow* Logger::sliding_state_window_ = NULL; |
| 304 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 305 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 306 | bool Logger::IsEnabled() { |
| 307 | return Log::IsEnabled(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 308 | } |
| 309 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 310 | |
| 311 | void Logger::ProfilerBeginEvent() { |
| 312 | if (!Log::IsEnabled()) return; |
| 313 | LogMessageBuilder msg; |
| 314 | msg.Append("profiler,\"begin\",%d\n", kSamplingIntervalMs); |
| 315 | msg.WriteToLogFile(); |
| 316 | } |
| 317 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 318 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 319 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 320 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 321 | void Logger::Preamble(const char* content) { |
| 322 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 323 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 324 | LogMessageBuilder msg; |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 325 | msg.WriteCStringToLogFile(content); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 326 | #endif |
| 327 | } |
| 328 | |
| 329 | |
| 330 | void Logger::StringEvent(const char* name, const char* value) { |
| 331 | #ifdef ENABLE_LOGGING_AND_PROFILING |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 332 | if (FLAG_log) UncheckedStringEvent(name, value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 333 | #endif |
| 334 | } |
| 335 | |
| 336 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 337 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 338 | void Logger::UncheckedStringEvent(const char* name, const char* value) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 339 | if (!Log::IsEnabled()) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 340 | LogMessageBuilder msg; |
| 341 | msg.Append("%s,\"%s\"\n", name, value); |
| 342 | msg.WriteToLogFile(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 343 | } |
| 344 | #endif |
| 345 | |
| 346 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 347 | void Logger::IntEvent(const char* name, int value) { |
| 348 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 349 | if (!Log::IsEnabled() || !FLAG_log) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 350 | LogMessageBuilder msg; |
| 351 | msg.Append("%s,%d\n", name, value); |
| 352 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 353 | #endif |
| 354 | } |
| 355 | |
| 356 | |
| 357 | void Logger::HandleEvent(const char* name, Object** location) { |
| 358 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 359 | if (!Log::IsEnabled() || !FLAG_log_handles) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 360 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 361 | msg.Append("%s,0x%" V8PRIxPTR "\n", name, location); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 362 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 363 | #endif |
| 364 | } |
| 365 | |
| 366 | |
| 367 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 368 | // ApiEvent is private so all the calls come from the Logger class. It is the |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 369 | // caller's responsibility to ensure that log is enabled and that |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | // FLAG_log_api is true. |
| 371 | void Logger::ApiEvent(const char* format, ...) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 372 | ASSERT(Log::IsEnabled() && FLAG_log_api); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 373 | LogMessageBuilder msg; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 374 | va_list ap; |
| 375 | va_start(ap, format); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 376 | msg.Append(format, ap); |
| 377 | va_end(ap); |
| 378 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 379 | } |
| 380 | #endif |
| 381 | |
| 382 | |
| 383 | void Logger::ApiNamedSecurityCheck(Object* key) { |
| 384 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 385 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 386 | if (key->IsString()) { |
| 387 | SmartPointer<char> str = |
| 388 | String::cast(key)->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 389 | ApiEvent("api,check-security,\"%s\"\n", *str); |
| 390 | } else if (key->IsUndefined()) { |
| 391 | ApiEvent("api,check-security,undefined\n"); |
| 392 | } else { |
| 393 | ApiEvent("api,check-security,['no-name']\n"); |
| 394 | } |
| 395 | #endif |
| 396 | } |
| 397 | |
| 398 | |
| 399 | void Logger::SharedLibraryEvent(const char* library_path, |
| 400 | unsigned start, |
| 401 | unsigned end) { |
| 402 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 403 | if (!Log::IsEnabled() || !FLAG_prof) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 404 | LogMessageBuilder msg; |
| 405 | msg.Append("shared-library,\"%s\",0x%08x,0x%08x\n", library_path, |
| 406 | start, end); |
| 407 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 408 | #endif |
| 409 | } |
| 410 | |
| 411 | |
| 412 | void Logger::SharedLibraryEvent(const wchar_t* library_path, |
| 413 | unsigned start, |
| 414 | unsigned end) { |
| 415 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 416 | if (!Log::IsEnabled() || !FLAG_prof) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 417 | LogMessageBuilder msg; |
| 418 | msg.Append("shared-library,\"%ls\",0x%08x,0x%08x\n", library_path, |
| 419 | start, end); |
| 420 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 421 | #endif |
| 422 | } |
| 423 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 424 | |
| 425 | #ifdef ENABLE_LOGGING_AND_PROFILING |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 426 | void Logger::LogRegExpSource(Handle<JSRegExp> regexp) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 427 | // Prints "/" + re.source + "/" + |
| 428 | // (re.global?"g":"") + (re.ignorecase?"i":"") + (re.multiline?"m":"") |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 429 | LogMessageBuilder msg; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 430 | |
| 431 | Handle<Object> source = GetProperty(regexp, "source"); |
| 432 | if (!source->IsString()) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 433 | msg.Append("no source"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 434 | return; |
| 435 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 436 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 437 | switch (regexp->TypeTag()) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 438 | case JSRegExp::ATOM: |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 439 | msg.Append('a'); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 440 | break; |
| 441 | default: |
| 442 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 443 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 444 | msg.Append('/'); |
| 445 | msg.AppendDetailed(*Handle<String>::cast(source), false); |
| 446 | msg.Append('/'); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 447 | |
| 448 | // global flag |
| 449 | Handle<Object> global = GetProperty(regexp, "global"); |
| 450 | if (global->IsTrue()) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 451 | msg.Append('g'); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 452 | } |
| 453 | // ignorecase flag |
| 454 | Handle<Object> ignorecase = GetProperty(regexp, "ignoreCase"); |
| 455 | if (ignorecase->IsTrue()) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 456 | msg.Append('i'); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 457 | } |
| 458 | // multiline flag |
| 459 | Handle<Object> multiline = GetProperty(regexp, "multiline"); |
| 460 | if (multiline->IsTrue()) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 461 | msg.Append('m'); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 462 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 463 | |
| 464 | msg.WriteToLogFile(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 465 | } |
| 466 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 467 | |
| 468 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 469 | void Logger::RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 470 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 471 | if (!Log::IsEnabled() || !FLAG_log_regexp) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 472 | LogMessageBuilder msg; |
| 473 | msg.Append("regexp-compile,"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 474 | LogRegExpSource(regexp); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 475 | msg.Append(in_cache ? ",hit\n" : ",miss\n"); |
| 476 | msg.WriteToLogFile(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 477 | #endif |
| 478 | } |
| 479 | |
| 480 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 481 | void Logger::LogRuntime(Vector<const char> format, JSArray* args) { |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 482 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 483 | if (!Log::IsEnabled() || !FLAG_log_runtime) return; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 484 | HandleScope scope; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 485 | LogMessageBuilder msg; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 486 | for (int i = 0; i < format.length(); i++) { |
| 487 | char c = format[i]; |
| 488 | if (c == '%' && i <= format.length() - 2) { |
| 489 | i++; |
| 490 | ASSERT('0' <= format[i] && format[i] <= '9'); |
| 491 | Object* obj = args->GetElement(format[i] - '0'); |
| 492 | i++; |
| 493 | switch (format[i]) { |
| 494 | case 's': |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 495 | msg.AppendDetailed(String::cast(obj), false); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 496 | break; |
| 497 | case 'S': |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 498 | msg.AppendDetailed(String::cast(obj), true); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 499 | break; |
| 500 | case 'r': |
| 501 | Logger::LogRegExpSource(Handle<JSRegExp>(JSRegExp::cast(obj))); |
| 502 | break; |
| 503 | case 'x': |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 504 | msg.Append("0x%x", Smi::cast(obj)->value()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 505 | break; |
| 506 | case 'i': |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 507 | msg.Append("%i", Smi::cast(obj)->value()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 508 | break; |
| 509 | default: |
| 510 | UNREACHABLE(); |
| 511 | } |
| 512 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 513 | msg.Append(c); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 516 | msg.Append('\n'); |
| 517 | msg.WriteToLogFile(); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 518 | #endif |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 522 | void Logger::ApiIndexedSecurityCheck(uint32_t index) { |
| 523 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 524 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 525 | ApiEvent("api,check-security,%u\n", index); |
| 526 | #endif |
| 527 | } |
| 528 | |
| 529 | |
| 530 | void Logger::ApiNamedPropertyAccess(const char* tag, |
| 531 | JSObject* holder, |
| 532 | Object* name) { |
| 533 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 534 | ASSERT(name->IsString()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 535 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 536 | String* class_name_obj = holder->class_name(); |
| 537 | SmartPointer<char> class_name = |
| 538 | class_name_obj->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 539 | SmartPointer<char> property_name = |
| 540 | String::cast(name)->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 541 | Logger::ApiEvent("api,%s,\"%s\",\"%s\"\n", tag, *class_name, *property_name); |
| 542 | #endif |
| 543 | } |
| 544 | |
| 545 | void Logger::ApiIndexedPropertyAccess(const char* tag, |
| 546 | JSObject* holder, |
| 547 | uint32_t index) { |
| 548 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 549 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | String* class_name_obj = holder->class_name(); |
| 551 | SmartPointer<char> class_name = |
| 552 | class_name_obj->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 553 | Logger::ApiEvent("api,%s,\"%s\",%u\n", tag, *class_name, index); |
| 554 | #endif |
| 555 | } |
| 556 | |
| 557 | void Logger::ApiObjectAccess(const char* tag, JSObject* object) { |
| 558 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 559 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 560 | String* class_name_obj = object->class_name(); |
| 561 | SmartPointer<char> class_name = |
| 562 | class_name_obj->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 563 | Logger::ApiEvent("api,%s,\"%s\"\n", tag, *class_name); |
| 564 | #endif |
| 565 | } |
| 566 | |
| 567 | |
| 568 | void Logger::ApiEntryCall(const char* name) { |
| 569 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 570 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | Logger::ApiEvent("api,%s\n", name); |
| 572 | #endif |
| 573 | } |
| 574 | |
| 575 | |
| 576 | void Logger::NewEvent(const char* name, void* object, size_t size) { |
| 577 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 578 | if (!Log::IsEnabled() || !FLAG_log) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 579 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 580 | msg.Append("new,%s,0x%" V8PRIxPTR ",%u\n", name, object, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 581 | static_cast<unsigned int>(size)); |
| 582 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 583 | #endif |
| 584 | } |
| 585 | |
| 586 | |
| 587 | void Logger::DeleteEvent(const char* name, void* object) { |
| 588 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 589 | if (!Log::IsEnabled() || !FLAG_log) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 590 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 591 | msg.Append("delete,%s,0x%" V8PRIxPTR "\n", name, object); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 592 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 593 | #endif |
| 594 | } |
| 595 | |
| 596 | |
| 597 | void Logger::CodeCreateEvent(const char* tag, Code* code, const char* comment) { |
| 598 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 599 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 600 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 601 | msg.Append("code-creation,%s,0x%" V8PRIxPTR ",%d,\"", tag, code->address(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 602 | code->ExecutableSize()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | for (const char* p = comment; *p != '\0'; p++) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 604 | if (*p == '"') { |
| 605 | msg.Append('\\'); |
| 606 | } |
| 607 | msg.Append(*p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 608 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 609 | msg.Append('"'); |
| 610 | msg.Append('\n'); |
| 611 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 612 | #endif |
| 613 | } |
| 614 | |
| 615 | |
| 616 | void Logger::CodeCreateEvent(const char* tag, Code* code, String* name) { |
| 617 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 618 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 619 | LogMessageBuilder msg; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | SmartPointer<char> str = |
| 621 | name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 622 | msg.Append("code-creation,%s,0x%" V8PRIxPTR ",%d,\"%s\"\n", |
| 623 | tag, code->address(), code->ExecutableSize(), *str); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 624 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | #endif |
| 626 | } |
| 627 | |
| 628 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 629 | void Logger::CodeCreateEvent(const char* tag, Code* code, String* name, |
| 630 | String* source, int line) { |
| 631 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 632 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 633 | LogMessageBuilder msg; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 634 | SmartPointer<char> str = |
| 635 | name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 636 | SmartPointer<char> sourcestr = |
| 637 | source->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 638 | msg.Append("code-creation,%s,0x%" V8PRIxPTR ",%d,\"%s %s:%d\"\n", |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 639 | tag, code->address(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 640 | code->ExecutableSize(), |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 641 | *str, *sourcestr, line); |
| 642 | msg.WriteToLogFile(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 643 | #endif |
| 644 | } |
| 645 | |
| 646 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 647 | void Logger::CodeCreateEvent(const char* tag, Code* code, int args_count) { |
| 648 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 649 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 650 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 651 | msg.Append("code-creation,%s,0x%" V8PRIxPTR ",%d,\"args_count: %d\"\n", tag, |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 652 | code->address(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 653 | code->ExecutableSize(), |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 654 | args_count); |
| 655 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 656 | #endif |
| 657 | } |
| 658 | |
| 659 | |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 660 | void Logger::RegExpCodeCreateEvent(Code* code, String* source) { |
| 661 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 662 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 663 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 664 | msg.Append("code-creation,%s,0x%" V8PRIxPTR ",%d,\"", "RegExp", |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 665 | code->address(), |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 666 | code->ExecutableSize()); |
| 667 | msg.AppendDetailed(source, false); |
| 668 | msg.Append("\"\n"); |
| 669 | msg.WriteToLogFile(); |
| 670 | #endif |
| 671 | } |
| 672 | |
| 673 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 674 | void Logger::CodeAllocateEvent(Code* code, Assembler* assem) { |
| 675 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 676 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 677 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 678 | msg.Append("code-allocate,0x%" V8PRIxPTR ",0x%" V8PRIxPTR "\n", |
| 679 | code->address(), |
| 680 | assem); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 681 | msg.WriteToLogFile(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 682 | #endif |
| 683 | } |
| 684 | |
| 685 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 686 | void Logger::CodeMoveEvent(Address from, Address to) { |
| 687 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 688 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 689 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 690 | msg.Append("code-move,0x%" V8PRIxPTR ",0x%" V8PRIxPTR "\n", from, to); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 691 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 692 | #endif |
| 693 | } |
| 694 | |
| 695 | |
| 696 | void Logger::CodeDeleteEvent(Address from) { |
| 697 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 698 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 699 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 700 | msg.Append("code-delete,0x%" V8PRIxPTR "\n", from); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 701 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | #endif |
| 703 | } |
| 704 | |
| 705 | |
| 706 | void Logger::ResourceEvent(const char* name, const char* tag) { |
| 707 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 708 | if (!Log::IsEnabled() || !FLAG_log) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 709 | LogMessageBuilder msg; |
| 710 | msg.Append("%s,%s,", name, tag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 711 | |
| 712 | uint32_t sec, usec; |
| 713 | if (OS::GetUserTime(&sec, &usec) != -1) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 714 | msg.Append("%d,%d,", sec, usec); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 715 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 716 | msg.Append("%.0f", OS::TimeCurrentMillis()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 717 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 718 | msg.Append('\n'); |
| 719 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 720 | #endif |
| 721 | } |
| 722 | |
| 723 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 724 | void Logger::SuspectReadEvent(String* name, Object* obj) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 725 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 726 | if (!Log::IsEnabled() || !FLAG_log_suspect) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 727 | LogMessageBuilder msg; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 728 | String* class_name = obj->IsJSObject() |
| 729 | ? JSObject::cast(obj)->class_name() |
| 730 | : Heap::empty_string(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 731 | msg.Append("suspect-read,"); |
| 732 | msg.Append(class_name); |
| 733 | msg.Append(','); |
| 734 | msg.Append('"'); |
| 735 | msg.Append(name); |
| 736 | msg.Append('"'); |
| 737 | msg.Append('\n'); |
| 738 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 739 | #endif |
| 740 | } |
| 741 | |
| 742 | |
| 743 | void Logger::HeapSampleBeginEvent(const char* space, const char* kind) { |
| 744 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 745 | if (!Log::IsEnabled() || !FLAG_log_gc) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 746 | LogMessageBuilder msg; |
| 747 | msg.Append("heap-sample-begin,\"%s\",\"%s\"\n", space, kind); |
| 748 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 749 | #endif |
| 750 | } |
| 751 | |
| 752 | |
| 753 | void Logger::HeapSampleEndEvent(const char* space, const char* kind) { |
| 754 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 755 | if (!Log::IsEnabled() || !FLAG_log_gc) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 756 | LogMessageBuilder msg; |
| 757 | msg.Append("heap-sample-end,\"%s\",\"%s\"\n", space, kind); |
| 758 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 759 | #endif |
| 760 | } |
| 761 | |
| 762 | |
| 763 | void Logger::HeapSampleItemEvent(const char* type, int number, int bytes) { |
| 764 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 765 | if (!Log::IsEnabled() || !FLAG_log_gc) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 766 | LogMessageBuilder msg; |
| 767 | msg.Append("heap-sample-item,%s,%d,%d\n", type, number, bytes); |
| 768 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 769 | #endif |
| 770 | } |
| 771 | |
| 772 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 773 | void Logger::DebugTag(const char* call_site_tag) { |
| 774 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 775 | if (!Log::IsEnabled() || !FLAG_log) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 776 | LogMessageBuilder msg; |
| 777 | msg.Append("debug-tag,%s\n", call_site_tag); |
| 778 | msg.WriteToLogFile(); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 779 | #endif |
| 780 | } |
| 781 | |
| 782 | |
| 783 | void Logger::DebugEvent(const char* event_type, Vector<uint16_t> parameter) { |
| 784 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 785 | if (!Log::IsEnabled() || !FLAG_log) return; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 786 | StringBuilder s(parameter.length() + 1); |
| 787 | for (int i = 0; i < parameter.length(); ++i) { |
| 788 | s.AddCharacter(static_cast<char>(parameter[i])); |
| 789 | } |
| 790 | char* parameter_string = s.Finalize(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 791 | LogMessageBuilder msg; |
| 792 | msg.Append("debug-queue-event,%s,%15.3f,%s\n", |
| 793 | event_type, |
| 794 | OS::TimeCurrentMillis(), |
| 795 | parameter_string); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 796 | DeleteArray(parameter_string); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 797 | msg.WriteToLogFile(); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 798 | #endif |
| 799 | } |
| 800 | |
| 801 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 802 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 803 | void Logger::TickEvent(TickSample* sample, bool overflow) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 804 | if (!Log::IsEnabled() || !FLAG_prof) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 805 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 806 | msg.Append("tick,0x%" V8PRIxPTR ",0x%" V8PRIxPTR ",%d", |
| 807 | sample->pc, sample->sp, static_cast<int>(sample->state)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 808 | if (overflow) { |
| 809 | msg.Append(",overflow"); |
| 810 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 811 | for (int i = 0; i < sample->frames_count; ++i) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 812 | msg.Append(",0x%" V8PRIxPTR, sample->stack[i]); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 813 | } |
| 814 | msg.Append('\n'); |
| 815 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 816 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 817 | |
| 818 | |
| 819 | bool Logger::IsProfilerPaused() { |
| 820 | return profiler_->paused(); |
| 821 | } |
| 822 | |
| 823 | |
| 824 | void Logger::PauseProfiler() { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 825 | if (profiler_->paused()) { |
| 826 | return; |
| 827 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 828 | profiler_->pause(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 829 | if (FLAG_prof_lazy) { |
| 830 | if (!FLAG_sliding_state_window) ticker_->Stop(); |
| 831 | FLAG_log_code = false; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 832 | // Must be the same message as Log::kDynamicBufferSeal. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 833 | LOG(UncheckedStringEvent("profiler", "pause")); |
| 834 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | |
| 838 | void Logger::ResumeProfiler() { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 839 | if (!profiler_->paused() || !Log::IsEnabled()) { |
| 840 | return; |
| 841 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 842 | if (FLAG_prof_lazy) { |
| 843 | LOG(UncheckedStringEvent("profiler", "resume")); |
| 844 | FLAG_log_code = true; |
| 845 | LogCompiledFunctions(); |
| 846 | if (!FLAG_sliding_state_window) ticker_->Start(); |
| 847 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 848 | profiler_->resume(); |
| 849 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 850 | |
| 851 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 852 | // This function can be called when Log's mutex is acquired, |
| 853 | // either from main or Profiler's thread. |
| 854 | void Logger::StopLoggingAndProfiling() { |
| 855 | Log::stop(); |
| 856 | PauseProfiler(); |
| 857 | } |
| 858 | |
| 859 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 860 | bool Logger::IsProfilerSamplerActive() { |
| 861 | return ticker_->IsActive(); |
| 862 | } |
| 863 | |
| 864 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 865 | int Logger::GetLogLines(int from_pos, char* dest_buf, int max_size) { |
| 866 | return Log::GetLogLines(from_pos, dest_buf, max_size); |
| 867 | } |
| 868 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 869 | |
| 870 | void Logger::LogCompiledFunctions() { |
| 871 | HandleScope scope; |
| 872 | Handle<SharedFunctionInfo>* sfis = NULL; |
| 873 | int compiled_funcs_count = 0; |
| 874 | |
| 875 | { |
| 876 | AssertNoAllocation no_alloc; |
| 877 | |
| 878 | HeapIterator iterator; |
| 879 | while (iterator.has_next()) { |
| 880 | HeapObject* obj = iterator.next(); |
| 881 | ASSERT(obj != NULL); |
| 882 | if (obj->IsSharedFunctionInfo() |
| 883 | && SharedFunctionInfo::cast(obj)->is_compiled()) { |
| 884 | ++compiled_funcs_count; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | sfis = NewArray< Handle<SharedFunctionInfo> >(compiled_funcs_count); |
| 889 | iterator.reset(); |
| 890 | |
| 891 | int i = 0; |
| 892 | while (iterator.has_next()) { |
| 893 | HeapObject* obj = iterator.next(); |
| 894 | ASSERT(obj != NULL); |
| 895 | if (obj->IsSharedFunctionInfo() |
| 896 | && SharedFunctionInfo::cast(obj)->is_compiled()) { |
| 897 | sfis[i++] = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj)); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | // During iteration, there can be heap allocation due to |
| 903 | // GetScriptLineNumber call. |
| 904 | for (int i = 0; i < compiled_funcs_count; ++i) { |
| 905 | Handle<SharedFunctionInfo> shared = sfis[i]; |
| 906 | Handle<String> name(String::cast(shared->name())); |
| 907 | Handle<String> func_name(name->length() > 0 ? |
| 908 | *name : shared->inferred_name()); |
| 909 | if (shared->script()->IsScript()) { |
| 910 | Handle<Script> script(Script::cast(shared->script())); |
| 911 | if (script->name()->IsString()) { |
| 912 | Handle<String> script_name(String::cast(script->name())); |
| 913 | int line_num = GetScriptLineNumber(script, shared->start_position()); |
| 914 | if (line_num > 0) { |
| 915 | line_num += script->line_offset()->value() + 1; |
| 916 | LOG(CodeCreateEvent("LazyCompile", shared->code(), *func_name, |
| 917 | *script_name, line_num)); |
| 918 | } else { |
| 919 | // Can't distinguish enum and script here, so always use Script. |
| 920 | LOG(CodeCreateEvent("Script", shared->code(), *script_name)); |
| 921 | } |
| 922 | continue; |
| 923 | } |
| 924 | } |
| 925 | // If no script or script has no name. |
| 926 | LOG(CodeCreateEvent("LazyCompile", shared->code(), *func_name)); |
| 927 | } |
| 928 | |
| 929 | DeleteArray(sfis); |
| 930 | } |
| 931 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 932 | #endif |
| 933 | |
| 934 | |
| 935 | bool Logger::Setup() { |
| 936 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 937 | // --log-all enables all the log flags. |
| 938 | if (FLAG_log_all) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 939 | FLAG_log_runtime = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 940 | FLAG_log_api = true; |
| 941 | FLAG_log_code = true; |
| 942 | FLAG_log_gc = true; |
| 943 | FLAG_log_suspect = true; |
| 944 | FLAG_log_handles = true; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 945 | FLAG_log_regexp = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | // --prof implies --log-code. |
| 949 | if (FLAG_prof) FLAG_log_code = true; |
| 950 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 951 | // --prof_lazy controls --log-code, implies --noprof_auto. |
| 952 | if (FLAG_prof_lazy) { |
| 953 | FLAG_log_code = false; |
| 954 | FLAG_prof_auto = false; |
| 955 | } |
| 956 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 957 | bool open_log_file = FLAG_log || FLAG_log_runtime || FLAG_log_api |
| 958 | || FLAG_log_code || FLAG_log_gc || FLAG_log_handles || FLAG_log_suspect |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 959 | || FLAG_log_regexp || FLAG_log_state_changes || FLAG_prof_lazy; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 960 | |
| 961 | // If we're logging anything, we need to open the log file. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 962 | if (open_log_file) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 963 | if (strcmp(FLAG_logfile, "-") == 0) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 964 | Log::OpenStdout(); |
| 965 | } else if (strcmp(FLAG_logfile, "*") == 0) { |
| 966 | Log::OpenMemoryBuffer(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 967 | } else if (strchr(FLAG_logfile, '%') != NULL) { |
| 968 | // If there's a '%' in the log file name we have to expand |
| 969 | // placeholders. |
| 970 | HeapStringAllocator allocator; |
| 971 | StringStream stream(&allocator); |
| 972 | for (const char* p = FLAG_logfile; *p; p++) { |
| 973 | if (*p == '%') { |
| 974 | p++; |
| 975 | switch (*p) { |
| 976 | case '\0': |
| 977 | // If there's a % at the end of the string we back up |
| 978 | // one character so we can escape the loop properly. |
| 979 | p--; |
| 980 | break; |
| 981 | case 't': { |
| 982 | // %t expands to the current time in milliseconds. |
| 983 | uint32_t time = static_cast<uint32_t>(OS::TimeCurrentMillis()); |
| 984 | stream.Add("%u", time); |
| 985 | break; |
| 986 | } |
| 987 | case '%': |
| 988 | // %% expands (contracts really) to %. |
| 989 | stream.Put('%'); |
| 990 | break; |
| 991 | default: |
| 992 | // All other %'s expand to themselves. |
| 993 | stream.Put('%'); |
| 994 | stream.Put(*p); |
| 995 | break; |
| 996 | } |
| 997 | } else { |
| 998 | stream.Put(*p); |
| 999 | } |
| 1000 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1001 | SmartPointer<const char> expanded = stream.ToCString(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1002 | Log::OpenFile(*expanded); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1003 | } else { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1004 | Log::OpenFile(FLAG_logfile); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1005 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1008 | current_state_ = &bottom_state_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1009 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame^] | 1010 | ticker_ = new Ticker(kSamplingIntervalMs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1011 | |
| 1012 | if (FLAG_sliding_state_window && sliding_state_window_ == NULL) { |
| 1013 | sliding_state_window_ = new SlidingStateWindow(); |
| 1014 | } |
| 1015 | |
| 1016 | if (FLAG_prof) { |
| 1017 | profiler_ = new Profiler(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1018 | if (!FLAG_prof_auto) |
| 1019 | profiler_->pause(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1020 | profiler_->Engage(); |
| 1021 | } |
| 1022 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 1023 | LogMessageBuilder::set_write_failure_handler(StopLoggingAndProfiling); |
| 1024 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1025 | return true; |
| 1026 | |
| 1027 | #else |
| 1028 | return false; |
| 1029 | #endif |
| 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | void Logger::TearDown() { |
| 1034 | #ifdef ENABLE_LOGGING_AND_PROFILING |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 1035 | LogMessageBuilder::set_write_failure_handler(NULL); |
| 1036 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1037 | // Stop the profiler before closing the file. |
| 1038 | if (profiler_ != NULL) { |
| 1039 | profiler_->Disengage(); |
| 1040 | delete profiler_; |
| 1041 | profiler_ = NULL; |
| 1042 | } |
| 1043 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1044 | delete sliding_state_window_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1045 | sliding_state_window_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1046 | |
| 1047 | delete ticker_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1048 | ticker_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1049 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1050 | Log::Close(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1051 | #endif |
| 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | void Logger::EnableSlidingStateWindow() { |
| 1056 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 1057 | // If the ticker is NULL, Logger::Setup has not been called yet. In |
| 1058 | // that case, we set the sliding_state_window flag so that the |
| 1059 | // sliding window computation will be started when Logger::Setup is |
| 1060 | // called. |
| 1061 | if (ticker_ == NULL) { |
| 1062 | FLAG_sliding_state_window = true; |
| 1063 | return; |
| 1064 | } |
| 1065 | // Otherwise, if the sliding state window computation has not been |
| 1066 | // started we do it now. |
| 1067 | if (sliding_state_window_ == NULL) { |
| 1068 | sliding_state_window_ = new SlidingStateWindow(); |
| 1069 | } |
| 1070 | #endif |
| 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | // |
| 1075 | // VMState class implementation. A simple stack of VM states held by the |
| 1076 | // logger and partially threaded through the call stack. States are pushed by |
| 1077 | // VMState construction and popped by destruction. |
| 1078 | // |
| 1079 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 1080 | static const char* StateToString(StateTag state) { |
| 1081 | switch (state) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1082 | case JS: |
| 1083 | return "JS"; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1084 | case GC: |
| 1085 | return "GC"; |
| 1086 | case COMPILER: |
| 1087 | return "COMPILER"; |
| 1088 | case OTHER: |
| 1089 | return "OTHER"; |
| 1090 | default: |
| 1091 | UNREACHABLE(); |
| 1092 | return NULL; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | VMState::VMState(StateTag state) { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1097 | #if !defined(ENABLE_HEAP_PROTECTION) |
| 1098 | // When not protecting the heap, there is no difference between |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1099 | // EXTERNAL and OTHER. As an optimization in that case, we will not |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1100 | // perform EXTERNAL->OTHER transitions through the API. We thus |
| 1101 | // compress the two states into one. |
| 1102 | if (state == EXTERNAL) state = OTHER; |
| 1103 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1104 | state_ = state; |
| 1105 | previous_ = Logger::current_state_; |
| 1106 | Logger::current_state_ = this; |
| 1107 | |
| 1108 | if (FLAG_log_state_changes) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1109 | LOG(UncheckedStringEvent("Entering", StateToString(state_))); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1110 | if (previous_ != NULL) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1111 | LOG(UncheckedStringEvent("From", StateToString(previous_->state_))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1114 | |
| 1115 | #ifdef ENABLE_HEAP_PROTECTION |
| 1116 | if (FLAG_protect_heap && previous_ != NULL) { |
| 1117 | if (state_ == EXTERNAL) { |
| 1118 | // We are leaving V8. |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1119 | ASSERT(previous_->state_ != EXTERNAL); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1120 | Heap::Protect(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1121 | } else if (previous_->state_ == EXTERNAL) { |
| 1122 | // We are entering V8. |
| 1123 | Heap::Unprotect(); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | VMState::~VMState() { |
| 1131 | Logger::current_state_ = previous_; |
| 1132 | |
| 1133 | if (FLAG_log_state_changes) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1134 | LOG(UncheckedStringEvent("Leaving", StateToString(state_))); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1135 | if (previous_ != NULL) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1136 | LOG(UncheckedStringEvent("To", StateToString(previous_->state_))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1139 | |
| 1140 | #ifdef ENABLE_HEAP_PROTECTION |
| 1141 | if (FLAG_protect_heap && previous_ != NULL) { |
| 1142 | if (state_ == EXTERNAL) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1143 | // We are reentering V8. |
| 1144 | ASSERT(previous_->state_ != EXTERNAL); |
| 1145 | Heap::Unprotect(); |
| 1146 | } else if (previous_->state_ == EXTERNAL) { |
| 1147 | // We are leaving V8. |
| 1148 | Heap::Protect(); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1152 | } |
| 1153 | #endif |
| 1154 | |
| 1155 | } } // namespace v8::internal |