1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "fp_unwinder.h"
17
18 #include <libunwind.h>
19 #include <libunwind_i-ohos.h>
20
21 #include "dfx_config.h"
22 #include "dfx_define.h"
23 #include "dfx_util.h"
24 #include "stack_util.h"
25 #include "unwind_define.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 #undef LOG_DOMAIN
31 #undef LOG_TAG
32 #define LOG_DOMAIN 0xD002D11
33 #define LOG_TAG "DfxFpUnwinder"
34 }
35
FpUnwinder()36 FpUnwinder::FpUnwinder()
37 {
38 frames_.clear();
39 GetSelfStackRange(stackBottom_, stackTop_);
40 }
41
FpUnwinder(uintptr_t pcs[],int32_t sz)42 FpUnwinder::FpUnwinder(uintptr_t pcs[], int32_t sz)
43 {
44 for (int32_t i = 0; i < sz; i++) {
45 DfxFrame frame;
46 frame.index = i;
47 frame.pc = i == 0 ? pcs[i] : pcs[i] - 4; // 4 : aarch64 instruction size
48 frames_.emplace_back(frame);
49 }
50 }
51
~FpUnwinder()52 FpUnwinder::~FpUnwinder()
53 {
54 frames_.clear();
55 }
56
GetFrames() const57 const std::vector<DfxFrame>& FpUnwinder::GetFrames() const
58 {
59 return frames_;
60 }
61
DlIteratePhdrCallback(struct dl_phdr_info * info,size_t size,void * data)62 int FpUnwinder::DlIteratePhdrCallback(struct dl_phdr_info *info, size_t size, void *data)
63 {
64 auto frame = static_cast<DfxFrame*>(data);
65 const Elf_W(Phdr) *phdr = info->dlpi_phdr;
66 for (int n = info->dlpi_phnum; --n >= 0; phdr++) {
67 if (phdr->p_type == PT_LOAD) {
68 Elf_W(Addr) vaddr = phdr->p_vaddr + info->dlpi_addr;
69 if (frame->pc >= vaddr && frame->pc < vaddr + phdr->p_memsz) {
70 frame->relPc = frame->pc - info->dlpi_addr;
71 frame->mapName = std::string(info->dlpi_name);
72 return 1; // let dl_iterate_phdr break
73 }
74 }
75 }
76 return 0;
77 }
78
UnwindWithContext(unw_context_t & context,size_t skipFrameNum,size_t maxFrameNums)79 bool FpUnwinder::UnwindWithContext(unw_context_t& context, size_t skipFrameNum, size_t maxFrameNums)
80 {
81 std::shared_ptr<DfxRegs> dfxregs = DfxRegs::Create();
82 #if defined(__arm__)
83 dfxregs->fp_ = context.regs[REG_ARM_R11];
84 dfxregs->pc_ = context.regs[REG_ARM_R15];
85 #elif defined(__aarch64__)
86 dfxregs->fp_ = context.uc_mcontext.regs[REG_AARCH64_X29];
87 dfxregs->pc_ = context.uc_mcontext.pc;
88 #elif defined(__riscv) && __riscv_xlen == 64
89 dfxregs->fp_ = context.uc_mcontext.__gregs[REG_RISCV64_X8];
90 dfxregs->pc_ = context.uc_mcontext.__gregs[REG_RISCV64_X1];
91 #else
92 #pragma message("Unsupported architecture")
93 #endif
94
95 return Unwind(dfxregs, skipFrameNum, maxFrameNums);
96 }
97
Unwind(size_t skipFrameNum,size_t maxFrameNums)98 bool FpUnwinder::Unwind(size_t skipFrameNum, size_t maxFrameNums)
99 {
100 std::shared_ptr<DfxRegs> dfxregs = DfxRegs::Create();
101 uintptr_t regs[FP_MINI_REGS_SIZE] = {0};
102 dfxregs->GetFramePointerMiniRegs(regs);
103 dfxregs->fp_ = regs[0]; // 0 : index of x29 or r11 register
104 dfxregs->pc_ = regs[3]; // 3 : index of x32 or r15 register
105 return Unwind(dfxregs, skipFrameNum, maxFrameNums);
106 }
107
Unwind(const std::shared_ptr<DfxRegs> & dfxregs,size_t skipFrameNum,size_t maxFrameNums)108 bool FpUnwinder::Unwind(const std::shared_ptr<DfxRegs> &dfxregs, size_t skipFrameNum, size_t maxFrameNums)
109 {
110 uintptr_t fp = dfxregs->fp_;
111 uintptr_t pc = dfxregs->pc_;
112
113 size_t index = 0;
114 size_t curIndex = 0;
115 do {
116 if (index < skipFrameNum) {
117 index++;
118 continue;
119 }
120 curIndex = index - skipFrameNum;
121
122 DfxFrame frame;
123 frame.index = curIndex;
124 frame.pc = index == 0 ? pc : pc - 4; // 4 : aarch64 instruction size
125 frames_.emplace_back(frame);
126 index++;
127 } while (Step(fp, pc) && (curIndex < maxFrameNums));
128 return (frames_.size() > 0);
129 }
130
UpdateFrameInfo()131 void FpUnwinder::UpdateFrameInfo()
132 {
133 if (frames_.empty()) {
134 return;
135 }
136
137 auto it = frames_.begin();
138 while (it != frames_.end()) {
139 if (dl_iterate_phdr(FpUnwinder::DlIteratePhdrCallback, &(*it)) != 1) {
140 // clean up frames after first invalid frame
141 frames_.erase(it, frames_.end());
142 break;
143 }
144 it++;
145 }
146 }
147
Step(uintptr_t & fp,uintptr_t & pc)148 bool FpUnwinder::Step(uintptr_t& fp, uintptr_t& pc)
149 {
150 uintptr_t prevFp = fp;
151 if (IsValidFrame(prevFp, stackTop_, stackBottom_)) {
152 fp = *reinterpret_cast<uintptr_t*>(prevFp);
153 pc = *reinterpret_cast<uintptr_t*>(prevFp + sizeof(uintptr_t));
154 return true;
155 }
156 return false;
157 }
158 } // namespace HiviewDFX
159 } // namespace OHOS
160