1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci#ifndef UNWINDER_H
16800b99b8Sopenharmony_ci#define UNWINDER_H
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <string>
19800b99b8Sopenharmony_ci#include <memory>
20800b99b8Sopenharmony_ci#include <vector>
21800b99b8Sopenharmony_ci
22800b99b8Sopenharmony_ci#include "dfx_frame.h"
23800b99b8Sopenharmony_ci#include "dfx_maps.h"
24800b99b8Sopenharmony_ci#include "dfx_regs.h"
25800b99b8Sopenharmony_ci#include "unwind_context.h"
26800b99b8Sopenharmony_ci
27800b99b8Sopenharmony_cinamespace OHOS {
28800b99b8Sopenharmony_cinamespace HiviewDFX {
29800b99b8Sopenharmony_ciclass Unwinder {
30800b99b8Sopenharmony_cipublic:
31800b99b8Sopenharmony_ci    // for local
32800b99b8Sopenharmony_ci    Unwinder(bool needMaps = true);
33800b99b8Sopenharmony_ci    // for remote
34800b99b8Sopenharmony_ci    Unwinder(int pid, bool crash = true);
35800b99b8Sopenharmony_ci    Unwinder(int pid, int nspid, bool crash);
36800b99b8Sopenharmony_ci    // for customized
37800b99b8Sopenharmony_ci    Unwinder(std::shared_ptr<UnwindAccessors> accessors, bool local = false);
38800b99b8Sopenharmony_ci    ~Unwinder() = default;
39800b99b8Sopenharmony_ci
40800b99b8Sopenharmony_ci    // disallow copy and move
41800b99b8Sopenharmony_ci    Unwinder(const Unwinder&) = delete;
42800b99b8Sopenharmony_ci    Unwinder& operator=(const Unwinder&) = delete;
43800b99b8Sopenharmony_ci    Unwinder(Unwinder&&) noexcept = delete;
44800b99b8Sopenharmony_ci    Unwinder& operator=(Unwinder&&) noexcept = delete;
45800b99b8Sopenharmony_ci
46800b99b8Sopenharmony_ci    void EnableUnwindCache(bool enableCache);
47800b99b8Sopenharmony_ci
48800b99b8Sopenharmony_ci    void EnableFpCheckMapExec(bool enableFpCheckMapExec);
49800b99b8Sopenharmony_ci    void EnableFillFrames(bool enableFillFrames);
50800b99b8Sopenharmony_ci    void EnableMethodIdLocal(bool enableMethodIdLocal);
51800b99b8Sopenharmony_ci    void IgnoreMixstack(bool ignoreMixstack);
52800b99b8Sopenharmony_ci
53800b99b8Sopenharmony_ci    void SetRegs(std::shared_ptr<DfxRegs> regs);
54800b99b8Sopenharmony_ci    const std::shared_ptr<DfxRegs>& GetRegs() const;
55800b99b8Sopenharmony_ci
56800b99b8Sopenharmony_ci    const std::shared_ptr<DfxMaps>& GetMaps() const;
57800b99b8Sopenharmony_ci
58800b99b8Sopenharmony_ci    uint16_t GetLastErrorCode() const;
59800b99b8Sopenharmony_ci    uint64_t GetLastErrorAddr() const;
60800b99b8Sopenharmony_ci
61800b99b8Sopenharmony_ci    bool GetStackRange(uintptr_t& stackBottom, uintptr_t& stackTop);
62800b99b8Sopenharmony_ci
63800b99b8Sopenharmony_ci    bool UnwindLocalWithContext(const ucontext_t& context,
64800b99b8Sopenharmony_ci        size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
65800b99b8Sopenharmony_ci    bool UnwindLocalWithTid(const pid_t tid,
66800b99b8Sopenharmony_ci        size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
67800b99b8Sopenharmony_ci    bool UnwindLocal(bool withRegs = false, bool fpUnwind = false,
68800b99b8Sopenharmony_ci        size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
69800b99b8Sopenharmony_ci    bool UnwindRemote(pid_t tid = 0, bool withRegs = false,
70800b99b8Sopenharmony_ci        size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
71800b99b8Sopenharmony_ci    bool Unwind(void *ctx,
72800b99b8Sopenharmony_ci        size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
73800b99b8Sopenharmony_ci    bool UnwindByFp(void *ctx,
74800b99b8Sopenharmony_ci        size_t maxFrameNum = DEFAULT_MAX_FRAME_NUM, size_t skipFrameNum = 0);
75800b99b8Sopenharmony_ci
76800b99b8Sopenharmony_ci    bool Step(uintptr_t& pc, uintptr_t& sp, void *ctx);
77800b99b8Sopenharmony_ci    bool FpStep(uintptr_t& fp, uintptr_t& pc, void *ctx);
78800b99b8Sopenharmony_ci
79800b99b8Sopenharmony_ci    void AddFrame(DfxFrame& frame);
80800b99b8Sopenharmony_ci    const std::vector<DfxFrame>& GetFrames() const;
81800b99b8Sopenharmony_ci    const std::vector<uintptr_t>& GetPcs() const;
82800b99b8Sopenharmony_ci    void FillFrames(std::vector<DfxFrame>& frames);
83800b99b8Sopenharmony_ci    void FillFrame(DfxFrame& frame);
84800b99b8Sopenharmony_ci    void FillJsFrame(DfxFrame& frame);
85800b99b8Sopenharmony_ci    bool GetFrameByPc(uintptr_t pc, std::shared_ptr<DfxMaps> maps, DfxFrame& frame);
86800b99b8Sopenharmony_ci    void GetFramesByPcs(std::vector<DfxFrame>& frames, std::vector<uintptr_t> pcs);
87800b99b8Sopenharmony_ci    void SetIsJitCrashFlag(bool isCrash);
88800b99b8Sopenharmony_ci    int ArkWriteJitCodeToFile(int fd);
89800b99b8Sopenharmony_ci    const std::vector<uintptr_t>& GetJitCache(void);
90800b99b8Sopenharmony_ci    bool GetLockInfo(int32_t tid, char* buf, size_t sz);
91800b99b8Sopenharmony_ci    void SetFrames(std::vector<DfxFrame>& frames);
92800b99b8Sopenharmony_ci
93800b99b8Sopenharmony_ci    static bool GetSymbolByPc(uintptr_t pc, std::shared_ptr<DfxMaps> maps,
94800b99b8Sopenharmony_ci        std::string& funcName, uint64_t& funcOffset);
95800b99b8Sopenharmony_ci    static void GetLocalFramesByPcs(std::vector<DfxFrame>& frames, std::vector<uintptr_t> pcs);
96800b99b8Sopenharmony_ci    static std::string GetFramesStr(const std::vector<DfxFrame>& frames);
97800b99b8Sopenharmony_ci    static void FillLocalFrames(std::vector<DfxFrame>& frames);
98800b99b8Sopenharmony_ci
99800b99b8Sopenharmony_ci    static bool AccessMem(void* memory, uintptr_t addr, uintptr_t *val);
100800b99b8Sopenharmony_ciprivate:
101800b99b8Sopenharmony_ci    class Impl;
102800b99b8Sopenharmony_ci    std::shared_ptr<Impl> impl_;
103800b99b8Sopenharmony_ci};
104800b99b8Sopenharmony_ci} // namespace HiviewDFX
105800b99b8Sopenharmony_ci} // namespace OHOS
106800b99b8Sopenharmony_ci#endif
107