xref: /aosp_15_r20/external/angle/src/common/backtrace_utils.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2022 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // backtrace_utils.h:
7 //   Tools to extract the backtrace from the ANGLE code during execution.
8 //
9 
10 #ifndef COMMON_BACKTRACEUTILS_H_
11 #define COMMON_BACKTRACEUTILS_H_
12 
13 #include <string>
14 #include <vector>
15 #include "debug.h"
16 #include "hash_utils.h"
17 
18 namespace angle
19 {
20 
21 // Used to store the backtrace information, such as the stack addresses and symbols.
22 class BacktraceInfo
23 {
24   public:
BacktraceInfo()25     BacktraceInfo() {}
~BacktraceInfo()26     ~BacktraceInfo() {}
27 
clear()28     void clear()
29     {
30         mStackAddresses.clear();
31         mStackSymbols.clear();
32     }
33 
getSize()34     size_t getSize() const
35     {
36         ASSERT(mStackAddresses.size() == mStackSymbols.size());
37         return mStackAddresses.size();
38     }
39 
getStackAddresses()40     std::vector<void *> getStackAddresses() const { return mStackAddresses; }
getStackSymbols()41     std::vector<std::string> getStackSymbols() const { return mStackSymbols; }
42 
43     bool operator==(const BacktraceInfo &rhs) const
44     {
45         return mStackAddresses == rhs.mStackAddresses;
46     }
47 
48     bool operator<(const BacktraceInfo &rhs) const { return mStackAddresses < rhs.mStackAddresses; }
49 
getStackAddress(size_t index)50     void *getStackAddress(size_t index) const
51     {
52         ASSERT(index < mStackAddresses.size());
53         return mStackAddresses[index];
54     }
55 
getStackSymbol(size_t index)56     std::string getStackSymbol(size_t index) const
57     {
58         ASSERT(index < mStackSymbols.size());
59         return mStackSymbols[index];
60     }
61 
hash()62     size_t hash() const { return ComputeGenericHash(*this); }
63 
64     // Used to add the stack addresses and their corresponding symbols to the object, when
65     // angle_enable_unwind_backtrace_support is enabled on Android.
66     void populateBacktraceInfo(void **stackAddressBuffer, size_t stackAddressCount);
67 
68   private:
69     std::vector<void *> mStackAddresses;
70     std::vector<std::string> mStackSymbols;
71 };
72 
73 // Used to obtain the stack addresses and symbols from the device, when
74 // angle_enable_unwind_backtrace_support is enabled on Android. Otherwise , it returns an empty
75 // object.
76 BacktraceInfo getBacktraceInfo();
77 
78 // Used to print the stack addresses and symbols embedded in the BacktraceInfo object.
79 void printBacktraceInfo(BacktraceInfo backtraceInfo);
80 
81 }  // namespace angle
82 
83 // Introduce std::hash for BacktraceInfo so it can be used as key for angle::HashMap.
84 namespace std
85 {
86 template <>
87 struct hash<angle::BacktraceInfo>
88 {
89     size_t operator()(const angle::BacktraceInfo &key) const { return key.hash(); }
90 };
91 }  // namespace std
92 
93 #endif  // COMMON_BACKTRACEUTILS_H_
94