/src/mozilla-central/toolkit/components/backgroundhangmonitor/ThreadStackHelper.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef mozilla_ThreadStackHelper_h |
8 | | #define mozilla_ThreadStackHelper_h |
9 | | |
10 | | #ifdef MOZ_GECKO_PROFILER |
11 | | |
12 | | #include "js/ProfilingStack.h" |
13 | | #include "HangDetails.h" |
14 | | #include "nsThread.h" |
15 | | |
16 | | #include <stddef.h> |
17 | | |
18 | | #if defined(XP_LINUX) |
19 | | #include <signal.h> |
20 | | #include <semaphore.h> |
21 | | #include <sys/types.h> |
22 | | #elif defined(XP_WIN) |
23 | | #include <windows.h> |
24 | | #elif defined(XP_MACOSX) |
25 | | #include <mach/mach.h> |
26 | | #endif |
27 | | |
28 | | // Support profiling stack and native stack on these platforms. |
29 | | #if defined(XP_LINUX) || defined(XP_WIN) || defined(XP_MACOSX) |
30 | | # define MOZ_THREADSTACKHELPER_PROFILING_STACK |
31 | | # define MOZ_THREADSTACKHELPER_NATIVE_STACK |
32 | | #endif |
33 | | |
34 | | |
35 | | // Android x86 builds consistently crash in the Background Hang Reporter. bug |
36 | | // 1368520. |
37 | | #if defined(__ANDROID__) |
38 | | # undef MOZ_THREADSTACKHELPER_PROFILING_STACK |
39 | | # undef MOZ_THREADSTACKHELPER_NATIVE_STACK |
40 | | #endif |
41 | | |
42 | | namespace mozilla { |
43 | | |
44 | | /** |
45 | | * ThreadStackHelper is used to retrieve the profiler's "profiling stack" of a |
46 | | * thread, as an alternative of using the profiler to take a profile. |
47 | | * The target thread first declares an ThreadStackHelper instance; |
48 | | * then another thread can call ThreadStackHelper::GetStack to retrieve |
49 | | * the profiling stack of the target thread at that instant. |
50 | | * |
51 | | * Only non-copying labels are included in the stack, which means labels |
52 | | * with custom text and markers are not included. |
53 | | */ |
54 | | class ThreadStackHelper : public ProfilerStackCollector |
55 | | { |
56 | | private: |
57 | | HangStack* mStackToFill; |
58 | | Array<char, nsThread::kRunnableNameBufSize>* mRunnableNameBuffer; |
59 | | size_t mMaxStackSize; |
60 | | size_t mMaxBufferSize; |
61 | | size_t mDesiredStackSize; |
62 | | size_t mDesiredBufferSize; |
63 | | |
64 | | bool PrepareStackBuffer(HangStack& aStack); |
65 | | |
66 | | public: |
67 | | /** |
68 | | * Create a ThreadStackHelper instance targeting the current thread. |
69 | | */ |
70 | | ThreadStackHelper(); |
71 | | |
72 | | /** |
73 | | * Retrieve the current interleaved stack of the thread associated with this ThreadStackHelper. |
74 | | * |
75 | | * @param aStack HangStack instance to be filled. |
76 | | * @param aRunnableName The name of the current runnable on the target thread. |
77 | | * @param aStackWalk If true, native stack frames will be collected |
78 | | * along with profiling stack frames. |
79 | | */ |
80 | | void GetStack(HangStack& aStack, nsACString& aRunnableName, bool aStackWalk); |
81 | | |
82 | | /** |
83 | | * Retrieve the thread's profiler thread ID. |
84 | | */ |
85 | 0 | int GetThreadId() const { return mThreadId; } |
86 | | |
87 | | protected: |
88 | | /** |
89 | | * ProfilerStackCollector |
90 | | */ |
91 | | virtual void SetIsMainThread() override; |
92 | | virtual void CollectNativeLeafAddr(void* aAddr) override; |
93 | | virtual void CollectJitReturnAddr(void* aAddr) override; |
94 | | virtual void CollectWasmFrame(const char* aLabel) override; |
95 | | virtual void CollectProfilingStackFrame(const js::ProfilingStackFrame& aEntry) override; |
96 | | |
97 | | private: |
98 | | void TryAppendFrame(mozilla::HangEntry aFrame); |
99 | | |
100 | | // The profiler's unique thread identifier for the target thread. |
101 | | int mThreadId; |
102 | | }; |
103 | | |
104 | | } // namespace mozilla |
105 | | |
106 | | #endif // MOZ_GECKO_PROFILER |
107 | | |
108 | | #endif // mozilla_ThreadStackHelper_h |