• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 EnableParseNativeSymbol(bool enableParseNativeSymbol);
51     void EnableJsvmstack(bool enableJsvmstack);
52     void IgnoreMixstack(bool ignoreMixstack);
53 
54     void SetRegs(std::shared_ptr<DfxRegs> regs);
55     const std::shared_ptr<DfxRegs>& GetRegs() const;
56 
57     const std::shared_ptr<DfxMaps>& GetMaps() const;
58 
59     uint16_t GetLastErrorCode() const;
60     uint64_t GetLastErrorAddr() const;
61 
62     bool GetStackRange(uintptr_t& stackBottom, uintptr_t& stackTop);
63 
64     bool UnwindLocalWithContext(const ucontext_t& context,
65         size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
66     bool UnwindLocalWithTid(const pid_t tid,
67         size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
68     bool UnwindLocalByOtherTid(const pid_t tid, bool fast = false,
69         size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
70     bool UnwindLocal(bool withRegs = false, bool fpUnwind = false,
71         size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0, bool enableArk = false);
72     bool UnwindRemote(pid_t tid = 0, bool withRegs = false,
73         size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
74     bool Unwind(void *ctx,
75         size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
76     bool UnwindByFp(void *ctx,
77         size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0, bool enableArk = false);
78 
79     bool Step(uintptr_t& pc, uintptr_t& sp, void *ctx);
80     bool FpStep(uintptr_t& fp, uintptr_t& pc, void *ctx);
81 
82     void AddFrame(DfxFrame& frame);
83     const std::vector<DfxFrame>& GetFrames() const;
84     const std::vector<uintptr_t>& GetPcs() const;
85     void FillFrames(std::vector<DfxFrame>& frames);
86     void FillFrame(DfxFrame& frame, bool needSymParse = true);
87     void ParseFrameSymbol(DfxFrame& frame);
88     void FillJsFrame(DfxFrame& frame);
89     bool GetFrameByPc(uintptr_t pc, std::shared_ptr<DfxMaps> maps, DfxFrame& frame);
90     void GetFramesByPcs(std::vector<DfxFrame>& frames, std::vector<uintptr_t> pcs);
91     void SetIsJitCrashFlag(bool isCrash);
92     int ArkWriteJitCodeToFile(int fd);
93     const std::vector<uintptr_t>& GetJitCache(void);
94     bool GetLockInfo(int32_t tid, char* buf, size_t sz);
95     void SetFrames(std::vector<DfxFrame>& frames);
96 
97     static bool GetSymbolByPc(uintptr_t pc, std::shared_ptr<DfxMaps> maps,
98         std::string& funcName, uint64_t& funcOffset);
99     static void GetLocalFramesByPcs(std::vector<DfxFrame>& frames, std::vector<uintptr_t> pcs);
100     static std::string GetFramesStr(const std::vector<DfxFrame>& frames);
101     static void FillLocalFrames(std::vector<DfxFrame>& frames);
102 
103     static bool AccessMem(void* memory, uintptr_t addr, uintptr_t *val);
104 private:
105     class Impl;
106     std::shared_ptr<Impl> impl_;
107 };
108 } // namespace HiviewDFX
109 } // namespace OHOS
110 #endif
111