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