| OLD | NEW | 
|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "src/sampler.h" | 5 #include "src/sampler.h" | 
| 6 | 6 | 
| 7 #if V8_OS_POSIX && !V8_OS_CYGWIN | 7 #if V8_OS_POSIX && !V8_OS_CYGWIN | 
| 8 | 8 | 
| 9 #define USE_SIGNALS | 9 #define USE_SIGNALS | 
| 10 | 10 | 
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 219 | 219 | 
| 220  private: | 220  private: | 
| 221   HANDLE profiled_thread_; | 221   HANDLE profiled_thread_; | 
| 222 }; | 222 }; | 
| 223 #endif | 223 #endif | 
| 224 | 224 | 
| 225 | 225 | 
| 226 #if defined(USE_SIMULATOR) | 226 #if defined(USE_SIMULATOR) | 
| 227 class SimulatorHelper { | 227 class SimulatorHelper { | 
| 228  public: | 228  public: | 
| 229   inline bool Init(Sampler* sampler, Isolate* isolate) { | 229   inline bool Init(Isolate* isolate) { | 
| 230     simulator_ = isolate->thread_local_top()->simulator_; | 230     simulator_ = isolate->thread_local_top()->simulator_; | 
| 231     // Check if there is active simulator. | 231     // Check if there is active simulator. | 
| 232     return simulator_ != NULL; | 232     return simulator_ != NULL; | 
| 233   } | 233   } | 
| 234 | 234 | 
| 235   inline void FillRegisters(RegisterState* state) { | 235   inline void FillRegisters(v8::RegisterState* state) { | 
| 236 #if V8_TARGET_ARCH_ARM | 236 #if V8_TARGET_ARCH_ARM | 
| 237     state->pc = reinterpret_cast<Address>(simulator_->get_pc()); | 237     state->pc = reinterpret_cast<Address>(simulator_->get_pc()); | 
| 238     state->sp = reinterpret_cast<Address>(simulator_->get_register( | 238     state->sp = reinterpret_cast<Address>(simulator_->get_register( | 
| 239         Simulator::sp)); | 239         Simulator::sp)); | 
| 240     state->fp = reinterpret_cast<Address>(simulator_->get_register( | 240     state->fp = reinterpret_cast<Address>(simulator_->get_register( | 
| 241         Simulator::r11)); | 241         Simulator::r11)); | 
| 242 #elif V8_TARGET_ARCH_ARM64 | 242 #elif V8_TARGET_ARCH_ARM64 | 
| 243     if (simulator_->sp() == 0 || simulator_->fp() == 0) { | 243     if (simulator_->sp() == 0 || simulator_->fp() == 0) { | 
| 244       // It possible that the simulator is interrupted while it is updating | 244       // It's possible that the simulator is interrupted while it is updating | 
| 245       // the sp or fp register. ARM64 simulator does this in two steps: | 245       // the sp or fp register. ARM64 simulator does this in two steps: | 
| 246       // first setting it to zero and then setting it to the new value. | 246       // first setting it to zero and then setting it to a new value. | 
| 247       // Bailout if sp/fp doesn't contain the new value. | 247       // Bailout if sp/fp doesn't contain the new value. | 
| 248       return; | 248       return; | 
| 249     } | 249     } | 
| 250     state->pc = reinterpret_cast<Address>(simulator_->pc()); | 250     state->pc = reinterpret_cast<Address>(simulator_->pc()); | 
| 251     state->sp = reinterpret_cast<Address>(simulator_->sp()); | 251     state->sp = reinterpret_cast<Address>(simulator_->sp()); | 
| 252     state->fp = reinterpret_cast<Address>(simulator_->fp()); | 252     state->fp = reinterpret_cast<Address>(simulator_->fp()); | 
| 253 #elif V8_TARGET_ARCH_MIPS | 253 #elif V8_TARGET_ARCH_MIPS | 
| 254     state->pc = reinterpret_cast<Address>(simulator_->get_pc()); | 254     state->pc = reinterpret_cast<Address>(simulator_->get_pc()); | 
| 255     state->sp = reinterpret_cast<Address>(simulator_->get_register( | 255     state->sp = reinterpret_cast<Address>(simulator_->get_register( | 
| 256         Simulator::sp)); | 256         Simulator::sp)); | 
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 346     return; | 346     return; | 
| 347   } | 347   } | 
| 348   if (v8::Locker::IsActive() && | 348   if (v8::Locker::IsActive() && | 
| 349       !isolate->thread_manager()->IsLockedByCurrentThread()) { | 349       !isolate->thread_manager()->IsLockedByCurrentThread()) { | 
| 350     return; | 350     return; | 
| 351   } | 351   } | 
| 352 | 352 | 
| 353   Sampler* sampler = isolate->logger()->sampler(); | 353   Sampler* sampler = isolate->logger()->sampler(); | 
| 354   if (sampler == NULL) return; | 354   if (sampler == NULL) return; | 
| 355 | 355 | 
| 356   RegisterState state; | 356   v8::RegisterState state; | 
| 357 | 357 | 
| 358 #if defined(USE_SIMULATOR) | 358 #if defined(USE_SIMULATOR) | 
| 359   SimulatorHelper helper; | 359   SimulatorHelper helper; | 
| 360   if (!helper.Init(sampler, isolate)) return; | 360   if (!helper.Init(isolate)) return; | 
| 361   helper.FillRegisters(&state); | 361   helper.FillRegisters(&state); | 
| 362   // It possible that the simulator is interrupted while it is updating | 362   // It possible that the simulator is interrupted while it is updating | 
| 363   // the sp or fp register. ARM64 simulator does this in two steps: | 363   // the sp or fp register. ARM64 simulator does this in two steps: | 
| 364   // first setting it to zero and then setting it to the new value. | 364   // first setting it to zero and then setting it to the new value. | 
| 365   // Bailout if sp/fp doesn't contain the new value. | 365   // Bailout if sp/fp doesn't contain the new value. | 
| 366   if (state.sp == 0 || state.fp == 0) return; | 366   if (state.sp == 0 || state.fp == 0) return; | 
| 367 #else | 367 #else | 
| 368   // Extracting the sample from the context is extremely machine dependent. | 368   // Extracting the sample from the context is extremely machine dependent. | 
| 369   ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | 369   ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | 
| 370 #if !V8_OS_OPENBSD | 370 #if !V8_OS_OPENBSD | 
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 570 | 570 | 
| 571 | 571 | 
| 572 base::Mutex* SamplerThread::mutex_ = NULL; | 572 base::Mutex* SamplerThread::mutex_ = NULL; | 
| 573 SamplerThread* SamplerThread::instance_ = NULL; | 573 SamplerThread* SamplerThread::instance_ = NULL; | 
| 574 | 574 | 
| 575 | 575 | 
| 576 // | 576 // | 
| 577 // StackTracer implementation | 577 // StackTracer implementation | 
| 578 // | 578 // | 
| 579 DISABLE_ASAN void TickSample::Init(Isolate* isolate, | 579 DISABLE_ASAN void TickSample::Init(Isolate* isolate, | 
| 580                                    const RegisterState& regs) { | 580                                    const v8::RegisterState& regs) { | 
| 581   DCHECK(isolate->IsInitialized()); | 581   DCHECK(isolate->IsInitialized()); | 
| 582   timestamp = base::TimeTicks::HighResolutionNow(); | 582   timestamp = base::TimeTicks::HighResolutionNow(); | 
| 583   pc = regs.pc; | 583   pc = reinterpret_cast<Address>(regs.pc); | 
| 584   state = isolate->current_vm_state(); | 584   state = isolate->current_vm_state(); | 
| 585 | 585 | 
| 586   // Avoid collecting traces while doing GC. | 586   // Avoid collecting traces while doing GC. | 
| 587   if (state == GC) return; | 587   if (state == GC) return; | 
| 588 | 588 | 
| 589   Address js_entry_sp = isolate->js_entry_sp(); | 589   Address js_entry_sp = isolate->js_entry_sp(); | 
| 590   if (js_entry_sp == 0) { | 590   if (js_entry_sp == 0) return;  // Not executing JS now. | 
| 591     // Not executing JS now. |  | 
| 592     return; |  | 
| 593   } |  | 
| 594 | 591 | 
| 595   ExternalCallbackScope* scope = isolate->external_callback_scope(); | 592   ExternalCallbackScope* scope = isolate->external_callback_scope(); | 
| 596   Address handler = Isolate::handler(isolate->thread_local_top()); | 593   Address handler = Isolate::handler(isolate->thread_local_top()); | 
| 597   // If there is a handler on top of the external callback scope then | 594   // If there is a handler on top of the external callback scope then | 
| 598   // we have already entrered JavaScript again and the external callback | 595   // we have already entrered JavaScript again and the external callback | 
| 599   // is not the top function. | 596   // is not the top function. | 
| 600   if (scope && scope->scope_address() < handler) { | 597   if (scope && scope->scope_address() < handler) { | 
| 601     external_callback = scope->callback(); | 598     external_callback = scope->callback(); | 
| 602     has_external_callback = true; | 599     has_external_callback = true; | 
| 603   } else { | 600   } else { | 
| 604     // Sample potential return address value for frameless invocation of | 601     // Sample potential return address value for frameless invocation of | 
| 605     // stubs (we'll figure out later, if this value makes sense). | 602     // stubs (we'll figure out later, if this value makes sense). | 
| 606     tos = Memory::Address_at(regs.sp); | 603     tos = Memory::Address_at(reinterpret_cast<Address>(regs.sp)); | 
| 607     has_external_callback = false; | 604     has_external_callback = false; | 
| 608   } | 605   } | 
| 609 | 606 | 
| 610   SafeStackFrameIterator it(isolate, regs.fp, regs.sp, js_entry_sp); | 607   SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp), | 
|  | 608                             reinterpret_cast<Address>(regs.sp), js_entry_sp); | 
| 611   top_frame_type = it.top_frame_type(); | 609   top_frame_type = it.top_frame_type(); | 
| 612   unsigned i = 0; | 610 | 
| 613   while (!it.done() && i < TickSample::kMaxFramesCount) { | 611   SampleInfo info; | 
| 614     stack[i++] = it.frame()->pc(); | 612   GetStackSample(isolate, regs, reinterpret_cast<void**>(&stack[0]), | 
| 615     it.Advance(); | 613                  kMaxFramesCount, &info); | 
| 616   } | 614   frames_count = static_cast<unsigned>(info.frames_count); | 
| 617   frames_count = i; |  | 
| 618 } | 615 } | 
| 619 | 616 | 
| 620 | 617 | 
|  | 618 void TickSample::GetStackSample(Isolate* isolate, const v8::RegisterState& regs, | 
|  | 619                                 void** frames, size_t frames_limit, | 
|  | 620                                 v8::SampleInfo* sample_info) { | 
|  | 621   DCHECK(isolate->IsInitialized()); | 
|  | 622   sample_info->frames_count = 0; | 
|  | 623   sample_info->vm_state = isolate->current_vm_state(); | 
|  | 624   if (sample_info->vm_state == GC) return; | 
|  | 625 | 
|  | 626   Address js_entry_sp = isolate->js_entry_sp(); | 
|  | 627   if (js_entry_sp == 0) return;  // Not executing JS now. | 
|  | 628 | 
|  | 629   SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp), | 
|  | 630                             reinterpret_cast<Address>(regs.sp), js_entry_sp); | 
|  | 631   size_t i = 0; | 
|  | 632   while (!it.done() && i < frames_limit) { | 
|  | 633     frames[i++] = it.frame()->pc(); | 
|  | 634     it.Advance(); | 
|  | 635   } | 
|  | 636   sample_info->frames_count = i; | 
|  | 637 } | 
|  | 638 | 
|  | 639 | 
| 621 void Sampler::SetUp() { | 640 void Sampler::SetUp() { | 
| 622 #if defined(USE_SIGNALS) | 641 #if defined(USE_SIGNALS) | 
| 623   SignalHandler::SetUp(); | 642   SignalHandler::SetUp(); | 
| 624 #endif | 643 #endif | 
| 625   SamplerThread::SetUp(); | 644   SamplerThread::SetUp(); | 
| 626 } | 645 } | 
| 627 | 646 | 
| 628 | 647 | 
| 629 void Sampler::TearDown() { | 648 void Sampler::TearDown() { | 
| 630   SamplerThread::TearDown(); | 649   SamplerThread::TearDown(); | 
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 675 | 694 | 
| 676 | 695 | 
| 677 void Sampler::DecreaseProfilingDepth() { | 696 void Sampler::DecreaseProfilingDepth() { | 
| 678 #if defined(USE_SIGNALS) | 697 #if defined(USE_SIGNALS) | 
| 679   SignalHandler::DecreaseSamplerCount(); | 698   SignalHandler::DecreaseSamplerCount(); | 
| 680 #endif | 699 #endif | 
| 681   base::NoBarrier_AtomicIncrement(&profiling_, -1); | 700   base::NoBarrier_AtomicIncrement(&profiling_, -1); | 
| 682 } | 701 } | 
| 683 | 702 | 
| 684 | 703 | 
| 685 void Sampler::SampleStack(const RegisterState& state) { | 704 void Sampler::SampleStack(const v8::RegisterState& state) { | 
| 686   TickSample* sample = isolate_->cpu_profiler()->StartTickSample(); | 705   TickSample* sample = isolate_->cpu_profiler()->StartTickSample(); | 
| 687   TickSample sample_obj; | 706   TickSample sample_obj; | 
| 688   if (sample == NULL) sample = &sample_obj; | 707   if (sample == NULL) sample = &sample_obj; | 
| 689   sample->Init(isolate_, state); | 708   sample->Init(isolate_, state); | 
| 690   if (is_counting_samples_) { | 709   if (is_counting_samples_) { | 
| 691     if (sample->state == JS || sample->state == EXTERNAL) { | 710     if (sample->state == JS || sample->state == EXTERNAL) { | 
| 692       ++js_and_external_sample_count_; | 711       ++js_and_external_sample_count_; | 
| 693     } | 712     } | 
| 694   } | 713   } | 
| 695   Tick(sample); | 714   Tick(sample); | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 707 } | 726 } | 
| 708 | 727 | 
| 709 #elif V8_OS_WIN || V8_OS_CYGWIN | 728 #elif V8_OS_WIN || V8_OS_CYGWIN | 
| 710 | 729 | 
| 711 void Sampler::DoSample() { | 730 void Sampler::DoSample() { | 
| 712   HANDLE profiled_thread = platform_data()->profiled_thread(); | 731   HANDLE profiled_thread = platform_data()->profiled_thread(); | 
| 713   if (profiled_thread == NULL) return; | 732   if (profiled_thread == NULL) return; | 
| 714 | 733 | 
| 715 #if defined(USE_SIMULATOR) | 734 #if defined(USE_SIMULATOR) | 
| 716   SimulatorHelper helper; | 735   SimulatorHelper helper; | 
| 717   if (!helper.Init(this, isolate())) return; | 736   if (!helper.Init(isolate())) return; | 
| 718 #endif | 737 #endif | 
| 719 | 738 | 
| 720   const DWORD kSuspendFailed = static_cast<DWORD>(-1); | 739   const DWORD kSuspendFailed = static_cast<DWORD>(-1); | 
| 721   if (SuspendThread(profiled_thread) == kSuspendFailed) return; | 740   if (SuspendThread(profiled_thread) == kSuspendFailed) return; | 
| 722 | 741 | 
| 723   // Context used for sampling the register state of the profiled thread. | 742   // Context used for sampling the register state of the profiled thread. | 
| 724   CONTEXT context; | 743   CONTEXT context; | 
| 725   memset(&context, 0, sizeof(context)); | 744   memset(&context, 0, sizeof(context)); | 
| 726   context.ContextFlags = CONTEXT_FULL; | 745   context.ContextFlags = CONTEXT_FULL; | 
| 727   if (GetThreadContext(profiled_thread, &context) != 0) { | 746   if (GetThreadContext(profiled_thread, &context) != 0) { | 
| 728     RegisterState state; | 747     v8::RegisterState state; | 
| 729 #if defined(USE_SIMULATOR) | 748 #if defined(USE_SIMULATOR) | 
| 730     helper.FillRegisters(&state); | 749     helper.FillRegisters(&state); | 
| 731 #else | 750 #else | 
| 732 #if V8_HOST_ARCH_X64 | 751 #if V8_HOST_ARCH_X64 | 
| 733     state.pc = reinterpret_cast<Address>(context.Rip); | 752     state.pc = reinterpret_cast<Address>(context.Rip); | 
| 734     state.sp = reinterpret_cast<Address>(context.Rsp); | 753     state.sp = reinterpret_cast<Address>(context.Rsp); | 
| 735     state.fp = reinterpret_cast<Address>(context.Rbp); | 754     state.fp = reinterpret_cast<Address>(context.Rbp); | 
| 736 #else | 755 #else | 
| 737     state.pc = reinterpret_cast<Address>(context.Eip); | 756     state.pc = reinterpret_cast<Address>(context.Eip); | 
| 738     state.sp = reinterpret_cast<Address>(context.Esp); | 757     state.sp = reinterpret_cast<Address>(context.Esp); | 
| 739     state.fp = reinterpret_cast<Address>(context.Ebp); | 758     state.fp = reinterpret_cast<Address>(context.Ebp); | 
| 740 #endif | 759 #endif | 
| 741 #endif  // USE_SIMULATOR | 760 #endif  // USE_SIMULATOR | 
| 742     SampleStack(state); | 761     SampleStack(state); | 
| 743   } | 762   } | 
| 744   ResumeThread(profiled_thread); | 763   ResumeThread(profiled_thread); | 
| 745 } | 764 } | 
| 746 | 765 | 
| 747 #endif  // USE_SIGNALS | 766 #endif  // USE_SIGNALS | 
| 748 | 767 | 
| 749 | 768 | 
| 750 } }  // namespace v8::internal | 769 } }  // namespace v8::internal | 
| OLD | NEW | 
|---|