• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dfx_define.h"
21 #include "dfx_util.h"
22 #include "dfx_regs_define.h"
23 #include "unwinder_define.h"
24 #include "dfx_config.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace {
29 #undef LOG_DOMAIN
30 #undef LOG_TAG
31 #define LOG_DOMAIN 0xD002D11
32 #define LOG_TAG "DfxFpUnwinder"
33 }
34 
FpUnwinder()35 FpUnwinder::FpUnwinder()
36 {
37     frames_.clear();
38     GetStackRange(stackBottom_, stackTop_);
39 }
40 
~FpUnwinder()41 FpUnwinder::~FpUnwinder()
42 {
43     frames_.clear();
44 }
45 
GetFrames() const46 const std::vector<DfxFrame>& FpUnwinder::GetFrames() const
47 {
48     return frames_;
49 }
50 
DlIteratePhdrCallback(struct dl_phdr_info * info,size_t size,void * data)51 int FpUnwinder::DlIteratePhdrCallback(struct dl_phdr_info *info, size_t size, void *data)
52 {
53     auto frame = static_cast<DfxFrame*>(data);
54     const Elf_W(Phdr) *phdr = info->dlpi_phdr;
55     for (int n = info->dlpi_phnum; --n >= 0; phdr++) {
56         if (phdr->p_type == PT_LOAD) {
57             Elf_W(Addr) vaddr = phdr->p_vaddr + info->dlpi_addr;
58             if (frame->pc >= vaddr && frame->pc < vaddr + phdr->p_memsz) {
59                 frame->relPc = frame->pc - info->dlpi_addr;
60                 frame->mapName = std::string(info->dlpi_name);
61                 return 1; // let dl_iterate_phdr break
62             }
63         }
64     }
65     return 0;
66 }
67 
UnwindWithContext(unw_context_t & context,size_t skipFrameNum)68 bool FpUnwinder::UnwindWithContext(unw_context_t& context, size_t skipFrameNum)
69 {
70     std::shared_ptr<DfxRegs> dfxregs = DfxRegs::Create();
71 #if defined(__arm__)
72     dfxregs->fp_ = context.regs[REG_ARM_R11];
73     dfxregs->pc_ = context.regs[REG_ARM_R15];
74 #elif defined(__aarch64__)
75     dfxregs->fp_ = context.uc_mcontext.regs[REG_AARCH64_X29];
76     dfxregs->pc_ = context.uc_mcontext.pc;
77 #else
78 #pragma message("Unsupported architecture")
79 #endif
80 
81     return Unwind(dfxregs, skipFrameNum);
82 }
83 
Unwind(size_t skipFrameNum)84 bool FpUnwinder::Unwind(size_t skipFrameNum)
85 {
86     std::shared_ptr<DfxRegs> dfxregs = DfxRegs::Create();
87     uintptr_t regs[FP_MINI_REGS_SIZE] = {0};
88     dfxregs->GetFramePointerMiniRegs(regs);
89     dfxregs->fp_ = regs[0]; // 0 : index of x29 or r11 register
90     dfxregs->pc_ = regs[3]; // 3 : index of x32 or r15 register
91     return Unwind(dfxregs, skipFrameNum);
92 }
93 
Unwind(const std::shared_ptr<DfxRegs> & dfxregs,size_t skipFrameNum)94 bool FpUnwinder::Unwind(const std::shared_ptr<DfxRegs> &dfxregs, size_t skipFrameNum)
95 {
96     uintptr_t fp = dfxregs->fp_;
97     uintptr_t pc = dfxregs->pc_;
98 
99     size_t index = 0;
100     size_t curIndex = 0;
101     do {
102         if (index < skipFrameNum) {
103             index++;
104             continue;
105         }
106         curIndex = index - skipFrameNum;
107 
108         DfxFrame frame;
109         frame.index = curIndex;
110         frame.pc = index == 0 ? pc : pc - 4; // 4 : aarch64 instruction size
111         frames_.emplace_back(frame);
112         index++;
113     } while (Step(fp, pc) && (curIndex < DfxConfig::GetConfig().maxFrameNums));
114     return (frames_.size() > 0);
115 }
116 
UpdateFrameInfo()117 void FpUnwinder::UpdateFrameInfo()
118 {
119     if (frames_.empty()) {
120         return;
121     }
122 
123     auto it = frames_.begin();
124     while (it != frames_.end()) {
125         if (dl_iterate_phdr(FpUnwinder::DlIteratePhdrCallback, &(*it)) != 1) {
126             // clean up frames after first invalid frame
127             frames_.erase(it, frames_.end());
128             break;
129         }
130         it++;
131     }
132 }
133 
Step(uintptr_t & fp,uintptr_t & pc)134 bool FpUnwinder::Step(uintptr_t& fp, uintptr_t& pc)
135 {
136     uintptr_t prevFp = fp;
137     if (IsValidFrame(prevFp, stackTop_, stackBottom_)) {
138         fp = *reinterpret_cast<uintptr_t*>(prevFp);
139         pc = *reinterpret_cast<uintptr_t*>(prevFp + sizeof(uintptr_t));
140         return true;
141     }
142     return false;
143 }
144 } // namespace HiviewDFX
145 } // namespace OHOS
146