blob: fcde3796cc484fe0c292a894ed0375d2d1e06300 [file] [log] [blame]
Yabin Cui9e402bb2015-09-22 04:46:57 +00001/*
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
30struct Space {
31 uint64_t start;
32 uint64_t end;
33 const uint8_t* data;
34
Yabin Cuif8808282017-12-12 18:04:10 -080035 Space() { Clear(); }
Yabin Cui9e402bb2015-09-22 04:46:57 +000036
37 void Clear();
38 size_t Read(uint64_t addr, uint8_t* buffer, size_t size);
39};
40
Yabin Cui5d991bc2016-11-15 17:47:09 -080041struct DebugFrameInfo;
Yabin Cui9e402bb2015-09-22 04:46:57 +000042
43class 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 Cui0ca49b02017-12-10 17:55:12 -080049 context_(nullptr),
50 is_debug_frame_used_(false) {
Yabin Cui9e402bb2015-09-22 04:46:57 +000051 stack_space_.start = stack.start;
52 stack_space_.end = stack.end;
53 stack_space_.data = stack.data;
54 }
55
Yabin Cui5d991bc2016-11-15 17:47:09 -080056 virtual ~BacktraceOffline() = default;
Yabin Cui9e402bb2015-09-22 04:46:57 +000057
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 Cui9e402bb2015-09-22 04:46:57 +000073 bool cache_file_;
74 ucontext_t* context_;
75 Space eh_frame_hdr_space_;
76 Space eh_frame_space_;
Yabin Cui5d991bc2016-11-15 17:47:09 -080077 Space arm_extab_space_;
78 Space arm_exidx_space_;
Yabin Cui9e402bb2015-09-22 04:46:57 +000079 Space stack_space_;
Yabin Cui0ca49b02017-12-10 17:55:12 -080080
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 Cui9e402bb2015-09-22 04:46:57 +000088};
89
90#endif // _LIBBACKTRACE_BACKTRACE_OFFLINE_H