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