1 /* 2 * Copyright (c) 2023-2024 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 UNWINDER_H 16 #define UNWINDER_H 17 18 #include <string> 19 #include <memory> 20 #include <vector> 21 22 #include "dfx_frame.h" 23 #include "dfx_maps.h" 24 #include "dfx_regs.h" 25 #include "unwind_context.h" 26 27 namespace OHOS { 28 namespace HiviewDFX { 29 class Unwinder { 30 public: 31 // for local 32 Unwinder(bool needMaps = true); 33 // for remote 34 Unwinder(int pid, bool crash = true); 35 Unwinder(int pid, int nspid, bool crash); 36 // for customized 37 Unwinder(std::shared_ptr<UnwindAccessors> accessors, bool local = false); 38 ~Unwinder() = default; 39 40 // disallow copy and move 41 Unwinder(const Unwinder&) = delete; 42 Unwinder& operator=(const Unwinder&) = delete; 43 Unwinder(Unwinder&&) noexcept = delete; 44 Unwinder& operator=(Unwinder&&) noexcept = delete; 45 46 void EnableUnwindCache(bool enableCache); 47 48 void EnableFpCheckMapExec(bool enableFpCheckMapExec); 49 void EnableFillFrames(bool enableFillFrames); 50 void EnableMethodIdLocal(bool enableMethodIdLocal); 51 void IgnoreMixstack(bool ignoreMixstack); 52 53 void SetRegs(std::shared_ptr<DfxRegs> regs); 54 const std::shared_ptr<DfxRegs>& GetRegs() const; 55 56 const std::shared_ptr<DfxMaps>& GetMaps() const; 57 58 uint16_t GetLastErrorCode() const; 59 uint64_t GetLastErrorAddr() const; 60 61 bool GetStackRange(uintptr_t& stackBottom, uintptr_t& stackTop); 62 63 bool UnwindLocalWithContext(const ucontext_t& context, 64 size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0); 65 bool UnwindLocalWithTid(const pid_t tid, 66 size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0); 67 bool UnwindLocal(bool withRegs = false, bool fpUnwind = false, 68 size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0); 69 bool UnwindRemote(pid_t tid = 0, bool withRegs = false, 70 size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0); 71 bool Unwind(void *ctx, 72 size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0); 73 bool UnwindByFp(void *ctx, 74 size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0); 75 76 bool Step(uintptr_t& pc, uintptr_t& sp, void *ctx); 77 bool FpStep(uintptr_t& fp, uintptr_t& pc, void *ctx); 78 79 void AddFrame(DfxFrame& frame); 80 const std::vector<DfxFrame>& GetFrames() const; 81 const std::vector<uintptr_t>& GetPcs() const; 82 void FillFrames(std::vector<DfxFrame>& frames); 83 void FillFrame(DfxFrame& frame); 84 void FillJsFrame(DfxFrame& frame); 85 bool GetFrameByPc(uintptr_t pc, std::shared_ptr<DfxMaps> maps, DfxFrame& frame); 86 void GetFramesByPcs(std::vector<DfxFrame>& frames, std::vector<uintptr_t> pcs); 87 void SetIsJitCrashFlag(bool isCrash); 88 int ArkWriteJitCodeToFile(int fd); 89 const std::vector<uintptr_t>& GetJitCache(void); 90 bool GetLockInfo(int32_t tid, char* buf, size_t sz); 91 void SetFrames(std::vector<DfxFrame>& frames); 92 93 static bool GetSymbolByPc(uintptr_t pc, std::shared_ptr<DfxMaps> maps, 94 std::string& funcName, uint64_t& funcOffset); 95 static void GetLocalFramesByPcs(std::vector<DfxFrame>& frames, std::vector<uintptr_t> pcs); 96 static std::string GetFramesStr(const std::vector<DfxFrame>& frames); 97 static void FillLocalFrames(std::vector<DfxFrame>& frames); 98 99 static bool AccessMem(void* memory, uintptr_t addr, uintptr_t *val); 100 private: 101 class Impl; 102 std::shared_ptr<Impl> impl_; 103 }; 104 } // namespace HiviewDFX 105 } // namespace OHOS 106 #endif 107