2013-11-22 23:17:30 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-06-30 19:39:45 +04:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-11-22 23:17:30 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_ThreadStackHelper_h
|
|
|
|
#define mozilla_ThreadStackHelper_h
|
|
|
|
|
|
|
|
#include "mozilla/ThreadHangStats.h"
|
|
|
|
|
|
|
|
#include "GeckoProfiler.h"
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#if defined(XP_LINUX)
|
|
|
|
#include <signal.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
#include <windows.h>
|
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-28 21:30:20 +04:00
|
|
|
// Support pseudostack on these platforms.
|
|
|
|
#if defined(XP_LINUX) || defined(XP_WIN) || defined(XP_MACOSX)
|
|
|
|
# ifdef MOZ_ENABLE_PROFILER_SPS
|
|
|
|
# define MOZ_THREADSTACKHELPER_PSEUDO
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2014-07-28 21:30:21 +04:00
|
|
|
#ifdef MOZ_THREADSTACKHELPER_PSEUDO
|
|
|
|
# define MOZ_THREADSTACKHELPER_NATIVE
|
|
|
|
# if defined(__i386__) || defined(_M_IX86)
|
|
|
|
# define MOZ_THREADSTACKHELPER_X86
|
|
|
|
# elif defined(__x86_64__) || defined(_M_X64)
|
|
|
|
# define MOZ_THREADSTACKHELPER_X64
|
|
|
|
# elif defined(__arm__) || defined(_M_ARM)
|
|
|
|
# define MOZ_THREADSTACKHELPER_ARM
|
|
|
|
# else
|
|
|
|
// Unsupported architecture
|
|
|
|
# undef MOZ_THREADSTACKHELPER_NATIVE
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ThreadStackHelper is used to retrieve the profiler pseudo-stack of a
|
|
|
|
* thread, as an alternative of using the profiler to take a profile.
|
|
|
|
* The target thread first declares an ThreadStackHelper instance;
|
|
|
|
* then another thread can call ThreadStackHelper::GetStack to retrieve
|
|
|
|
* the pseudo-stack of the target thread at that instant.
|
|
|
|
*
|
|
|
|
* Only non-copying labels are included in the stack, which means labels
|
|
|
|
* with custom text and markers are not included.
|
|
|
|
*/
|
|
|
|
class ThreadStackHelper
|
|
|
|
{
|
|
|
|
public:
|
2014-06-20 22:28:10 +04:00
|
|
|
typedef Telemetry::HangStack Stack;
|
2013-11-22 23:17:30 +04:00
|
|
|
|
|
|
|
private:
|
2014-07-28 21:30:20 +04:00
|
|
|
Stack* mStackToFill;
|
|
|
|
#ifdef MOZ_THREADSTACKHELPER_PSEUDO
|
2013-11-22 23:17:30 +04:00
|
|
|
const PseudoStack* const mPseudoStack;
|
2014-07-28 21:30:21 +04:00
|
|
|
#ifdef MOZ_THREADSTACKHELPER_NATIVE
|
|
|
|
class CodeModulesProvider;
|
|
|
|
class ThreadContext;
|
|
|
|
// Set to non-null if GetStack should get the thread context.
|
|
|
|
ThreadContext* mContextToFill;
|
|
|
|
intptr_t mThreadStackBase;
|
2013-11-25 18:24:00 +04:00
|
|
|
#endif
|
2013-11-22 23:17:30 +04:00
|
|
|
size_t mMaxStackSize;
|
2014-06-20 22:28:10 +04:00
|
|
|
size_t mMaxBufferSize;
|
2014-07-28 14:29:00 +04:00
|
|
|
#endif
|
2013-11-22 23:17:30 +04:00
|
|
|
|
|
|
|
bool PrepareStackBuffer(Stack& aStack);
|
|
|
|
void FillStackBuffer();
|
2014-07-28 21:30:21 +04:00
|
|
|
void FillThreadContext(void* aContext = nullptr);
|
2014-07-28 21:30:20 +04:00
|
|
|
#ifdef MOZ_THREADSTACKHELPER_PSEUDO
|
2014-06-07 02:39:40 +04:00
|
|
|
const char* AppendJSEntry(const volatile StackEntry* aEntry,
|
2014-06-20 22:28:10 +04:00
|
|
|
intptr_t& aAvailableBufferSize,
|
2014-06-07 02:39:40 +04:00
|
|
|
const char* aPrevLabel);
|
|
|
|
#endif
|
2014-07-28 21:30:21 +04:00
|
|
|
#ifdef MOZ_THREADSTACKHELPER_NATIVE
|
|
|
|
void GetThreadStackBase();
|
|
|
|
#endif
|
2013-11-22 23:17:30 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Initialize ThreadStackHelper. Must be called from main thread.
|
|
|
|
*/
|
|
|
|
static void Startup();
|
|
|
|
/**
|
|
|
|
* Uninitialize ThreadStackHelper. Must be called from main thread.
|
|
|
|
*/
|
|
|
|
static void Shutdown();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a ThreadStackHelper instance targeting the current thread.
|
|
|
|
*/
|
|
|
|
ThreadStackHelper();
|
|
|
|
|
|
|
|
~ThreadStackHelper();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the current pseudostack of the thread associated
|
|
|
|
* with this ThreadStackHelper.
|
|
|
|
*
|
|
|
|
* @param aStack Stack instance to be filled.
|
|
|
|
*/
|
|
|
|
void GetStack(Stack& aStack);
|
|
|
|
|
2014-07-28 21:30:21 +04:00
|
|
|
/**
|
|
|
|
* Retrieve the current native stack of the thread associated
|
|
|
|
* with this ThreadStackHelper.
|
|
|
|
*
|
|
|
|
* @param aNativeStack Stack instance to be filled.
|
|
|
|
*/
|
|
|
|
void GetNativeStack(Stack& aStack);
|
|
|
|
|
2013-11-22 23:17:30 +04:00
|
|
|
#if defined(XP_LINUX)
|
|
|
|
private:
|
|
|
|
static int sInitialized;
|
2014-07-08 22:53:17 +04:00
|
|
|
static int sFillStackSignum;
|
2013-11-22 23:17:30 +04:00
|
|
|
|
2014-07-08 22:53:17 +04:00
|
|
|
static void FillStackHandler(int aSignal, siginfo_t* aInfo, void* aContext);
|
2013-11-22 23:17:30 +04:00
|
|
|
|
2014-07-08 22:53:17 +04:00
|
|
|
sem_t mSem;
|
2013-11-22 23:17:30 +04:00
|
|
|
pid_t mThreadID;
|
|
|
|
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
private:
|
|
|
|
bool mInitialized;
|
|
|
|
HANDLE mThreadID;
|
|
|
|
|
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
private:
|
|
|
|
thread_act_t mThreadID;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_ThreadStackHelper_h
|