| 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> |
| Christopher Ferris | 15933b6 | 2018-02-22 19:06:42 -0800 | [diff] [blame] | 22 | #include <unordered_map> |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 23 | |
| 24 | #include "perf_regs.h" |
| 25 | |
| Christopher Ferris | 15933b6 | 2018-02-22 19:06:42 -0800 | [diff] [blame] | 26 | #include <backtrace/BacktraceMap.h> |
| 27 | |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 28 | namespace simpleperf { |
| 29 | struct ThreadEntry; |
| 30 | |
| 31 | struct UnwindingResult { |
| 32 | // time used for unwinding, in ns. |
| 33 | uint64_t used_time; |
| 34 | enum { |
| 35 | UNKNOWN_REASON, |
| 36 | EXCEED_MAX_FRAMES_LIMIT, |
| 37 | ACCESS_REG_FAILED, |
| 38 | ACCESS_STACK_FAILED, |
| 39 | ACCESS_MEM_FAILED, |
| 40 | FIND_PROC_INFO_FAILED, |
| 41 | EXECUTE_DWARF_INSTRUCTION_FAILED, |
| 42 | DIFFERENT_ARCH, |
| Yabin Cui | 750b373 | 2017-12-18 17:46:36 -0800 | [diff] [blame] | 43 | MAP_MISSING, |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 44 | } stop_reason; |
| 45 | union { |
| 46 | // for ACCESS_REG_FAILED |
| 47 | uint64_t regno; |
| 48 | // for ACCESS_MEM_FAILED and ACCESS_STACK_FAILED |
| 49 | uint64_t addr; |
| 50 | } stop_info; |
| Yabin Cui | 85faad9 | 2018-01-04 17:17:55 -0800 | [diff] [blame] | 51 | uint64_t stack_start; |
| 52 | uint64_t stack_end; |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | class OfflineUnwinder { |
| 56 | public: |
| Yabin Cui | 6e75e1b | 2018-02-06 13:42:16 -0800 | [diff] [blame] | 57 | OfflineUnwinder(bool collect_stat); |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 58 | |
| Yabin Cui | 6e75e1b | 2018-02-06 13:42:16 -0800 | [diff] [blame] | 59 | bool UnwindCallChain(const ThreadEntry& thread, const RegSet& regs, const char* stack, |
| 60 | size_t stack_size, std::vector<uint64_t>* ips, std::vector<uint64_t>* sps); |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 61 | |
| 62 | bool HasStat() const { |
| 63 | return collect_stat_; |
| 64 | } |
| 65 | |
| 66 | const UnwindingResult& GetUnwindingResult() const { |
| 67 | return unwinding_result_; |
| 68 | } |
| 69 | |
| 70 | private: |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 71 | bool collect_stat_; |
| 72 | UnwindingResult unwinding_result_; |
| Christopher Ferris | 15933b6 | 2018-02-22 19:06:42 -0800 | [diff] [blame] | 73 | |
| 74 | // Cache of the most recently used map. |
| 75 | struct CachedMap { |
| 76 | uint64_t version = 0u; |
| 77 | std::unique_ptr<BacktraceMap> map; |
| 78 | }; |
| 79 | // use unused attribute to pass mac build. |
| 80 | std::unordered_map<pid_t, CachedMap> cached_maps_ __attribute__((unused)); |
| Yabin Cui | 0338145 | 2017-12-13 11:31:53 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // namespace simpleperf |
| 84 | |
| 85 | #endif // SIMPLE_PERF_OFFLINE_UNWINDER_H_ |