/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // this port is based off of v8 svn revision 9837 /* static */ Thread::tid_t Thread::GetCurrentId() { return gettid(); } class PlatformData { public: explicit PlatformData(int aThreadId) : mProfiledThread(mach_thread_self()) { MOZ_COUNT_CTOR(PlatformData); } ~PlatformData() { // Deallocate Mach port for thread. mach_port_deallocate(mach_task_self(), mProfiledThread); MOZ_COUNT_DTOR(PlatformData); } thread_act_t ProfiledThread() { return mProfiledThread; } private: // Note: for mProfiledThread Mach primitives are used instead of pthread's // because the latter doesn't provide thread manipulation primitives required. // For details, consult "Mac OS X Internals" book, Section 7.3. thread_act_t mProfiledThread; }; //////////////////////////////////////////////////////////////////////// // BEGIN SamplerThread target specifics static void* ThreadEntry(void* aArg) { auto thread = static_cast(aArg); thread->Run(); return nullptr; } SamplerThread::SamplerThread(PSLockRef aLock, uint32_t aActivityGeneration, double aIntervalMilliseconds) : mActivityGeneration(aActivityGeneration) , mIntervalMicroseconds( std::max(1, int(floor(aIntervalMilliseconds * 1000 + 0.5)))) { MOZ_RELEASE_ASSERT(NS_IsMainThread()); pthread_attr_t* attr_ptr = nullptr; if (pthread_create(&mThread, attr_ptr, ThreadEntry, this) != 0) { MOZ_CRASH("pthread_create failed"); } } SamplerThread::~SamplerThread() { pthread_join(mThread, nullptr); } void SamplerThread::Stop(PSLockRef aLock) { MOZ_RELEASE_ASSERT(NS_IsMainThread()); } void SamplerThread::SleepMicro(uint32_t aMicroseconds) { usleep(aMicroseconds); // FIXME: the OSX 10.12 page for usleep says "The usleep() function is // obsolescent. Use nanosleep(2) instead." This implementation could be // merged with the linux-android version. Also, this doesn't handle the // case where the usleep call is interrupted by a signal. } void SamplerThread::SuspendAndSampleAndResumeThread(PSLockRef aLock, TickSample& aSample) { thread_act_t samplee_thread = aSample.mPlatformData->ProfiledThread(); //----------------------------------------------------------------// // Suspend the samplee thread and get its context. // We're using thread_suspend on OS X because pthread_kill (which is what we // at one time used on Linux) has less consistent performance and causes // strange crashes, see bug 1166778 and bug 1166808. thread_suspend // is also just a lot simpler to use. if (KERN_SUCCESS != thread_suspend(samplee_thread)) { return; } //----------------------------------------------------------------// // Sample the target thread. // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING // // The profiler's "critical section" begins here. We must be very careful // what we do here, or risk deadlock. See the corresponding comment in // platform-linux-android.cpp for details. #if defined(GP_ARCH_amd64) thread_state_flavor_t flavor = x86_THREAD_STATE64; x86_thread_state64_t state; mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT; # if __DARWIN_UNIX03 # define REGISTER_FIELD(name) __r ## name # else # define REGISTER_FIELD(name) r ## name # endif // __DARWIN_UNIX03 #elif defined(GP_ARCH_x86) thread_state_flavor_t flavor = i386_THREAD_STATE; i386_thread_state_t state; mach_msg_type_number_t count = i386_THREAD_STATE_COUNT; # if __DARWIN_UNIX03 # define REGISTER_FIELD(name) __e ## name # else # define REGISTER_FIELD(name) e ## name # endif // __DARWIN_UNIX03 #else # error Unsupported Mac OS X host architecture. #endif // GP_ARCH_* if (thread_get_state(samplee_thread, flavor, reinterpret_cast(&state), &count) == KERN_SUCCESS) { aSample.mPC = reinterpret_cast
(state.REGISTER_FIELD(ip)); aSample.mSP = reinterpret_cast
(state.REGISTER_FIELD(sp)); aSample.mFP = reinterpret_cast
(state.REGISTER_FIELD(bp)); Tick(aLock, ActivePS::Buffer(aLock), aSample); } #undef REGISTER_FIELD //----------------------------------------------------------------// // Resume the target thread. thread_resume(samplee_thread); // The profiler's critical section ends here. // // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING } // END SamplerThread target specifics //////////////////////////////////////////////////////////////////////// static void PlatformInit(PSLockRef aLock) { } void TickSample::PopulateContext(void* aContext) { MOZ_ASSERT(mIsSynchronous); MOZ_ASSERT(!aContext); // Note that this asm changes if PopulateContext's parameter list is altered #if defined(GP_ARCH_amd64) asm ( // Compute caller's %rsp by adding to %rbp: // 8 bytes for previous %rbp, 8 bytes for return address "leaq 0x10(%%rbp), %0\n\t" // Dereference %rbp to get previous %rbp "movq (%%rbp), %1\n\t" : "=r"(mSP), "=r"(mFP) ); #elif defined(GP_ARCH_x86) asm ( // Compute caller's %esp by adding to %ebp: // 4 bytes for aContext + 4 bytes for return address + // 4 bytes for previous %ebp "leal 0xc(%%ebp), %0\n\t" // Dereference %ebp to get previous %ebp "movl (%%ebp), %1\n\t" : "=r"(mSP), "=r"(mFP) ); #else # error "Unsupported architecture" #endif mPC = reinterpret_cast
(__builtin_extract_return_addr( __builtin_return_address(0))); }