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 #ifndef FP_UNWINDER_H 16 #define FP_UNWINDER_H 17 18 #include <memory> 19 #include <vector> 20 #include <link.h> 21 #include <unistd.h> 22 #include <libunwind.h> 23 #include "dfx_define.h" 24 #include "dfx_frame.h" 25 #include "dfx_regs.h" 26 27 namespace OHOS { 28 namespace HiviewDFX { 29 class FpUnwinder { 30 public: 31 FpUnwinder(); 32 FpUnwinder(uintptr_t pcs[], int32_t sz); 33 ~FpUnwinder(); 34 35 bool UnwindWithContext(unw_context_t& context, size_t skipFrameNum, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 36 bool Unwind(size_t skipFrameNum, size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 37 bool Unwind(const std::shared_ptr<DfxRegs> &dfxregs, size_t skipFrameNum, 38 size_t maxFrameNums = DEFAULT_MAX_FRAME_NUM); 39 40 void UpdateFrameInfo(); 41 const std::vector<DfxFrame>& GetFrames() const; 42 private: 43 bool Step(uintptr_t& fp, uintptr_t& pc); IsValidFrame(uintptr_t frame,uintptr_t stackTop,uintptr_t stackBottom)44 inline bool IsValidFrame(uintptr_t frame, uintptr_t stackTop, uintptr_t stackBottom) 45 { 46 return ((frame > stackBottom) && (frame < stackTop - sizeof(uintptr_t))); 47 } 48 static int DlIteratePhdrCallback(struct dl_phdr_info *info, size_t size, void *data); 49 50 private: 51 std::vector<DfxFrame> frames_; 52 uintptr_t stackBottom_; 53 uintptr_t stackTop_; 54 }; 55 } // namespace HiviewDFX 56 } // namespace OHOS 57 #endif 58