Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [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 _LIBBACKTRACE_UNWIND_OFFLINE_H |
| 18 | #define _LIBBACKTRACE_UNWIND_OFFLINE_H |
| 19 | |
| 20 | #include <libunwind.h> |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <ucontext.h> |
| 24 | |
| 25 | #include <unordered_map> |
| 26 | #include <unordered_set> |
| 27 | |
| 28 | #include <backtrace/Backtrace.h> |
| 29 | |
| 30 | struct Space { |
| 31 | uint64_t start; |
| 32 | uint64_t end; |
| 33 | const uint8_t* data; |
| 34 | |
Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 35 | Space() { Clear(); } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 36 | |
| 37 | void Clear(); |
| 38 | size_t Read(uint64_t addr, uint8_t* buffer, size_t size); |
| 39 | }; |
| 40 | |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 41 | struct DebugFrameInfo; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 42 | |
| 43 | class BacktraceOffline : public Backtrace { |
| 44 | public: |
| 45 | BacktraceOffline(pid_t pid, pid_t tid, BacktraceMap* map, const backtrace_stackinfo_t& stack, |
| 46 | bool cache_file) |
| 47 | : Backtrace(pid, tid, map), |
| 48 | cache_file_(cache_file), |
Yabin Cui | 0ca49b0 | 2017-12-10 17:55:12 -0800 | [diff] [blame] | 49 | context_(nullptr), |
| 50 | is_debug_frame_used_(false) { |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 51 | stack_space_.start = stack.start; |
| 52 | stack_space_.end = stack.end; |
| 53 | stack_space_.data = stack.data; |
| 54 | } |
| 55 | |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 56 | virtual ~BacktraceOffline() = default; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 57 | |
| 58 | bool Unwind(size_t num_ignore_frames, ucontext_t* context) override; |
| 59 | |
| 60 | bool ReadWord(uintptr_t ptr, word_t* out_value) override; |
| 61 | |
| 62 | size_t Read(uintptr_t addr, uint8_t* buffer, size_t bytes) override; |
| 63 | |
| 64 | bool FindProcInfo(unw_addr_space_t addr_space, uint64_t ip, unw_proc_info_t* proc_info, |
| 65 | int need_unwind_info); |
| 66 | |
| 67 | bool ReadReg(size_t reg_index, uint64_t* value); |
| 68 | |
| 69 | protected: |
| 70 | std::string GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) override; |
| 71 | DebugFrameInfo* GetDebugFrameInFile(const std::string& filename); |
| 72 | |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 73 | bool cache_file_; |
| 74 | ucontext_t* context_; |
| 75 | Space eh_frame_hdr_space_; |
| 76 | Space eh_frame_space_; |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 77 | Space arm_extab_space_; |
| 78 | Space arm_exidx_space_; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 79 | Space stack_space_; |
Yabin Cui | 0ca49b0 | 2017-12-10 17:55:12 -0800 | [diff] [blame] | 80 | |
| 81 | // is_debug_frame_used_ is to make sure we can try both .debug_frame and .ARM.exidx in |
| 82 | // FindProcInfo() on ARM. One example is EsxContext::Clear() in |
| 83 | // vendor/lib/egl/libGLESv2_adreno.so. EsxContext::Clear() appears in both .debug_frame and |
| 84 | // .ARM.exidx. However, libunwind fails to execute debug_frame instruction |
| 85 | // "DW_CFA_offset_extended: r265 at cfa-48". So we need to try .ARM.exidx to unwind that |
| 86 | // function. |
| 87 | bool is_debug_frame_used_; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | #endif // _LIBBACKTRACE_BACKTRACE_OFFLINE_H |