1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/threads/ThreadStackHelper.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_ThreadStackHelper_h 1.10 +#define mozilla_ThreadStackHelper_h 1.11 + 1.12 +#include "mozilla/ThreadHangStats.h" 1.13 + 1.14 +#include "GeckoProfiler.h" 1.15 + 1.16 +#include <stddef.h> 1.17 + 1.18 +#if defined(XP_LINUX) 1.19 +#include <signal.h> 1.20 +#include <semaphore.h> 1.21 +#include <sys/types.h> 1.22 +#elif defined(XP_WIN) 1.23 +#include <windows.h> 1.24 +#elif defined(XP_MACOSX) 1.25 +#include <mach/mach.h> 1.26 +#endif 1.27 + 1.28 +namespace mozilla { 1.29 + 1.30 +/** 1.31 + * ThreadStackHelper is used to retrieve the profiler pseudo-stack of a 1.32 + * thread, as an alternative of using the profiler to take a profile. 1.33 + * The target thread first declares an ThreadStackHelper instance; 1.34 + * then another thread can call ThreadStackHelper::GetStack to retrieve 1.35 + * the pseudo-stack of the target thread at that instant. 1.36 + * 1.37 + * Only non-copying labels are included in the stack, which means labels 1.38 + * with custom text and markers are not included. 1.39 + */ 1.40 +class ThreadStackHelper 1.41 +{ 1.42 +public: 1.43 + typedef Telemetry::HangHistogram::Stack Stack; 1.44 + 1.45 +private: 1.46 +#ifdef MOZ_ENABLE_PROFILER_SPS 1.47 + const PseudoStack* const mPseudoStack; 1.48 +#endif 1.49 + Stack mStackBuffer; 1.50 + size_t mMaxStackSize; 1.51 + 1.52 + bool PrepareStackBuffer(Stack& aStack); 1.53 + void FillStackBuffer(); 1.54 + 1.55 +public: 1.56 + /** 1.57 + * Initialize ThreadStackHelper. Must be called from main thread. 1.58 + */ 1.59 + static void Startup(); 1.60 + /** 1.61 + * Uninitialize ThreadStackHelper. Must be called from main thread. 1.62 + */ 1.63 + static void Shutdown(); 1.64 + 1.65 + /** 1.66 + * Create a ThreadStackHelper instance targeting the current thread. 1.67 + */ 1.68 + ThreadStackHelper(); 1.69 + 1.70 + ~ThreadStackHelper(); 1.71 + 1.72 + /** 1.73 + * Retrieve the current pseudostack of the thread associated 1.74 + * with this ThreadStackHelper. 1.75 + * 1.76 + * @param aStack Stack instance to be filled. 1.77 + */ 1.78 + void GetStack(Stack& aStack); 1.79 + 1.80 +#if defined(XP_LINUX) 1.81 +private: 1.82 + static int sInitialized; 1.83 + static sem_t sSem; 1.84 + static struct sigaction sOldSigAction; 1.85 + static ThreadStackHelper* sCurrent; 1.86 + 1.87 + static void SigAction(int aSignal, siginfo_t* aInfo, void* aContext); 1.88 + 1.89 + pid_t mThreadID; 1.90 + 1.91 +#elif defined(XP_WIN) 1.92 +private: 1.93 + bool mInitialized; 1.94 + HANDLE mThreadID; 1.95 + 1.96 +#elif defined(XP_MACOSX) 1.97 +private: 1.98 + thread_act_t mThreadID; 1.99 + 1.100 +#endif 1.101 +}; 1.102 + 1.103 +} // namespace mozilla 1.104 + 1.105 +#endif // mozilla_ThreadStackHelper_h