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