xref: /aosp_15_r20/system/extras/simpleperf/OfflineUnwinder.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #ifndef SIMPLE_PERF_OFFLINE_UNWINDER_H_
18*288bf522SAndroid Build Coastguard Worker #define SIMPLE_PERF_OFFLINE_UNWINDER_H_
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <memory>
21*288bf522SAndroid Build Coastguard Worker #include <vector>
22*288bf522SAndroid Build Coastguard Worker 
23*288bf522SAndroid Build Coastguard Worker #include "perf_regs.h"
24*288bf522SAndroid Build Coastguard Worker #include "thread_tree.h"
25*288bf522SAndroid Build Coastguard Worker 
26*288bf522SAndroid Build Coastguard Worker namespace simpleperf {
27*288bf522SAndroid Build Coastguard Worker struct ThreadEntry;
28*288bf522SAndroid Build Coastguard Worker 
29*288bf522SAndroid Build Coastguard Worker enum UnwindStackErrorCode : uint8_t {
30*288bf522SAndroid Build Coastguard Worker   ERROR_NONE,                   // No error.
31*288bf522SAndroid Build Coastguard Worker   ERROR_MEMORY_INVALID,         // Memory read failed.
32*288bf522SAndroid Build Coastguard Worker   ERROR_UNWIND_INFO,            // Unable to use unwind information to unwind.
33*288bf522SAndroid Build Coastguard Worker   ERROR_UNSUPPORTED,            // Encountered unsupported feature.
34*288bf522SAndroid Build Coastguard Worker   ERROR_INVALID_MAP,            // Unwind in an invalid map.
35*288bf522SAndroid Build Coastguard Worker   ERROR_MAX_FRAMES_EXCEEDED,    // The number of frames exceed the total allowed.
36*288bf522SAndroid Build Coastguard Worker   ERROR_REPEATED_FRAME,         // The last frame has the same pc/sp as the next.
37*288bf522SAndroid Build Coastguard Worker   ERROR_INVALID_ELF,            // Unwind in an invalid elf.
38*288bf522SAndroid Build Coastguard Worker   ERROR_THREAD_DOES_NOT_EXIST,  // Attempt to unwind a local thread that does
39*288bf522SAndroid Build Coastguard Worker                                 // not exist.
40*288bf522SAndroid Build Coastguard Worker   ERROR_THREAD_TIMEOUT,         // Timeout trying to unwind a local thread.
41*288bf522SAndroid Build Coastguard Worker   ERROR_SYSTEM_CALL,            // System call failed while unwinding.
42*288bf522SAndroid Build Coastguard Worker   ERROR_BAD_ARCH,               // Arch invalid (none, or mismatched).
43*288bf522SAndroid Build Coastguard Worker   ERROR_MAPS_PARSE,             // Failed to parse maps data.
44*288bf522SAndroid Build Coastguard Worker   ERROR_INVALID_PARAMETER,      // Invalid parameter passed to function.
45*288bf522SAndroid Build Coastguard Worker   ERROR_MAX = ERROR_INVALID_PARAMETER,
46*288bf522SAndroid Build Coastguard Worker };
47*288bf522SAndroid Build Coastguard Worker 
48*288bf522SAndroid Build Coastguard Worker struct UnwindingResult {
49*288bf522SAndroid Build Coastguard Worker   // time used for unwinding, in ns.
50*288bf522SAndroid Build Coastguard Worker   uint64_t used_time;
51*288bf522SAndroid Build Coastguard Worker   // unwindstack::LastErrorCode()
52*288bf522SAndroid Build Coastguard Worker   uint64_t error_code;
53*288bf522SAndroid Build Coastguard Worker   // unwindstack::LastErrorAddress()
54*288bf522SAndroid Build Coastguard Worker   uint64_t error_addr;
55*288bf522SAndroid Build Coastguard Worker   uint64_t stack_start;
56*288bf522SAndroid Build Coastguard Worker   uint64_t stack_end;
57*288bf522SAndroid Build Coastguard Worker };
58*288bf522SAndroid Build Coastguard Worker 
59*288bf522SAndroid Build Coastguard Worker class OfflineUnwinder {
60*288bf522SAndroid Build Coastguard Worker  public:
61*288bf522SAndroid Build Coastguard Worker   static constexpr const char* META_KEY_ARM64_PAC_MASK = "arm64_pac_mask";
62*288bf522SAndroid Build Coastguard Worker 
63*288bf522SAndroid Build Coastguard Worker   static std::unique_ptr<OfflineUnwinder> Create(bool collect_stat);
~OfflineUnwinder()64*288bf522SAndroid Build Coastguard Worker   virtual ~OfflineUnwinder() {}
65*288bf522SAndroid Build Coastguard Worker 
66*288bf522SAndroid Build Coastguard Worker   virtual bool UnwindCallChain(const ThreadEntry& thread, const RegSet& regs, const char* stack,
67*288bf522SAndroid Build Coastguard Worker                                size_t stack_size, std::vector<uint64_t>* ips,
68*288bf522SAndroid Build Coastguard Worker                                std::vector<uint64_t>* sps) = 0;
69*288bf522SAndroid Build Coastguard Worker 
GetUnwindingResult()70*288bf522SAndroid Build Coastguard Worker   const UnwindingResult& GetUnwindingResult() const { return unwinding_result_; }
71*288bf522SAndroid Build Coastguard Worker 
IsCallChainBrokenForIncompleteJITDebugInfo()72*288bf522SAndroid Build Coastguard Worker   bool IsCallChainBrokenForIncompleteJITDebugInfo() {
73*288bf522SAndroid Build Coastguard Worker     return is_callchain_broken_for_incomplete_jit_debug_info_;
74*288bf522SAndroid Build Coastguard Worker   }
75*288bf522SAndroid Build Coastguard Worker 
76*288bf522SAndroid Build Coastguard Worker   static void CollectMetaInfo(std::unordered_map<std::string, std::string>* info_map);
LoadMetaInfo(const std::unordered_map<std::string,std::string> &)77*288bf522SAndroid Build Coastguard Worker   virtual void LoadMetaInfo(const std::unordered_map<std::string, std::string>&) {}
78*288bf522SAndroid Build Coastguard Worker 
79*288bf522SAndroid Build Coastguard Worker  protected:
OfflineUnwinder()80*288bf522SAndroid Build Coastguard Worker   OfflineUnwinder() {}
81*288bf522SAndroid Build Coastguard Worker 
82*288bf522SAndroid Build Coastguard Worker   UnwindingResult unwinding_result_;
83*288bf522SAndroid Build Coastguard Worker   bool is_callchain_broken_for_incomplete_jit_debug_info_ = false;
84*288bf522SAndroid Build Coastguard Worker };
85*288bf522SAndroid Build Coastguard Worker 
86*288bf522SAndroid Build Coastguard Worker }  // namespace simpleperf
87*288bf522SAndroid Build Coastguard Worker 
88*288bf522SAndroid Build Coastguard Worker #endif  // SIMPLE_PERF_OFFLINE_UNWINDER_H_
89