1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2021-2023 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 DFX_REGS_H
16800b99b8Sopenharmony_ci#define DFX_REGS_H
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <cstdint>
19800b99b8Sopenharmony_ci#include <string>
20800b99b8Sopenharmony_ci#include <memory>
21800b99b8Sopenharmony_ci#include <sys/types.h>
22800b99b8Sopenharmony_ci#include <ucontext.h>
23800b99b8Sopenharmony_ci#include <vector>
24800b99b8Sopenharmony_ci#include "dfx_define.h"
25800b99b8Sopenharmony_ci#include "dfx_elf.h"
26800b99b8Sopenharmony_ci#include "dfx_memory.h"
27800b99b8Sopenharmony_ci#include "unwind_define.h"
28800b99b8Sopenharmony_ci
29800b99b8Sopenharmony_cinamespace OHOS {
30800b99b8Sopenharmony_cinamespace HiviewDFX {
31800b99b8Sopenharmony_ciclass DfxRegs {
32800b99b8Sopenharmony_cipublic:
33800b99b8Sopenharmony_ci    explicit DfxRegs() : regsData_(REG_LAST) {}
34800b99b8Sopenharmony_ci    virtual ~DfxRegs() = default;
35800b99b8Sopenharmony_ci
36800b99b8Sopenharmony_ci    static std::shared_ptr<DfxRegs> Create();
37800b99b8Sopenharmony_ci    static std::shared_ptr<DfxRegs> CreateFromUcontext(const ucontext_t& context);
38800b99b8Sopenharmony_ci    static std::shared_ptr<DfxRegs> CreateFromRegs(const UnwindMode mode, const uintptr_t* regs,
39800b99b8Sopenharmony_ci                                                   size_t size);
40800b99b8Sopenharmony_ci    static std::shared_ptr<DfxRegs> CreateRemoteRegs(pid_t pid);
41800b99b8Sopenharmony_ci    virtual void SetFromUcontext(const ucontext_t& context) = 0;
42800b99b8Sopenharmony_ci    virtual void SetFromFpMiniRegs(const uintptr_t* regs, const size_t size) = 0;
43800b99b8Sopenharmony_ci    virtual void SetFromQutMiniRegs(const uintptr_t* regs, const size_t size) = 0;
44800b99b8Sopenharmony_ci    virtual std::string PrintRegs() const = 0;
45800b99b8Sopenharmony_ci    virtual bool SetPcFromReturnAddress(std::shared_ptr<DfxMemory> memory) = 0;
46800b99b8Sopenharmony_ci    virtual bool StepIfSignalFrame(uintptr_t pc, std::shared_ptr<DfxMemory> memory) = 0;
47800b99b8Sopenharmony_ci
48800b99b8Sopenharmony_ci    inline uintptr_t& operator[](size_t idx) { return regsData_[idx]; }
49800b99b8Sopenharmony_ci
50800b99b8Sopenharmony_ci    void* RawData() { return regsData_.data(); }
51800b99b8Sopenharmony_ci    size_t RegsSize() const { return regsData_.size(); }
52800b99b8Sopenharmony_ci    std::vector<uintptr_t> GetRegsData() const;
53800b99b8Sopenharmony_ci    void SetRegsData(const std::vector<uintptr_t>& regsData);
54800b99b8Sopenharmony_ci    void SetRegsData(const uintptr_t* regs, const size_t size);
55800b99b8Sopenharmony_ci    uintptr_t* GetReg(size_t idx);
56800b99b8Sopenharmony_ci    void SetReg(const int idx, const uintptr_t* val);
57800b99b8Sopenharmony_ci
58800b99b8Sopenharmony_ci    uintptr_t GetSp() const;
59800b99b8Sopenharmony_ci    void SetSp(uintptr_t sp);
60800b99b8Sopenharmony_ci    uintptr_t GetPc() const;
61800b99b8Sopenharmony_ci    void SetPc(uintptr_t pc);
62800b99b8Sopenharmony_ci    uintptr_t GetFp() const;
63800b99b8Sopenharmony_ci    void SetFp(uintptr_t fp);
64800b99b8Sopenharmony_ci    void GetSpecialRegs(uintptr_t& fp, uintptr_t& lr, uintptr_t& sp, uintptr_t& pc) const;
65800b99b8Sopenharmony_ci    void SetSpecialRegs(uintptr_t fp, uintptr_t lr, uintptr_t sp, uintptr_t pc);
66800b99b8Sopenharmony_ci    std::string GetSpecialRegsName(uintptr_t val) const;
67800b99b8Sopenharmony_ci    std::string GetSpecialRegsNameByIndex(int index) const;
68800b99b8Sopenharmony_ci    std::string PrintSpecialRegs() const;
69800b99b8Sopenharmony_ciprotected:
70800b99b8Sopenharmony_ci    std::vector<uintptr_t> regsData_ {};
71800b99b8Sopenharmony_ci};
72800b99b8Sopenharmony_ci
73800b99b8Sopenharmony_ci#if defined(__arm__)
74800b99b8Sopenharmony_ciclass DfxRegsArm : public DfxRegs {
75800b99b8Sopenharmony_cipublic:
76800b99b8Sopenharmony_ci    DfxRegsArm() = default;
77800b99b8Sopenharmony_ci    ~DfxRegsArm() = default;
78800b99b8Sopenharmony_ci    void SetFromUcontext(const ucontext_t& context) override;
79800b99b8Sopenharmony_ci    void SetFromFpMiniRegs(const uintptr_t* regs, const size_t size) override;
80800b99b8Sopenharmony_ci    void SetFromQutMiniRegs(const uintptr_t* regs, const size_t size) override;
81800b99b8Sopenharmony_ci    std::string PrintRegs() const override;
82800b99b8Sopenharmony_ci    bool SetPcFromReturnAddress(std::shared_ptr<DfxMemory> memory) override;
83800b99b8Sopenharmony_ci    bool StepIfSignalFrame(uintptr_t pc, std::shared_ptr<DfxMemory> memory) override;
84800b99b8Sopenharmony_ci};
85800b99b8Sopenharmony_ci#endif
86800b99b8Sopenharmony_ci
87800b99b8Sopenharmony_ci#if defined(__aarch64__)
88800b99b8Sopenharmony_ciclass DfxRegsArm64 : public DfxRegs {
89800b99b8Sopenharmony_cipublic:
90800b99b8Sopenharmony_ci    DfxRegsArm64() = default;
91800b99b8Sopenharmony_ci    ~DfxRegsArm64() = default;
92800b99b8Sopenharmony_ci    void SetFromUcontext(const ucontext_t& context) override;
93800b99b8Sopenharmony_ci    void SetFromFpMiniRegs(const uintptr_t* regs, const size_t size) override;
94800b99b8Sopenharmony_ci    void SetFromQutMiniRegs(const uintptr_t* regs, const size_t size) override;
95800b99b8Sopenharmony_ci    std::string PrintRegs() const override;
96800b99b8Sopenharmony_ci    bool SetPcFromReturnAddress(std::shared_ptr<DfxMemory> memory) override;
97800b99b8Sopenharmony_ci    bool StepIfSignalFrame(uintptr_t pc, std::shared_ptr<DfxMemory> memory) override;
98800b99b8Sopenharmony_ci};
99800b99b8Sopenharmony_ci#endif
100800b99b8Sopenharmony_ci
101800b99b8Sopenharmony_ci#if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
102800b99b8Sopenharmony_ciclass DfxRegsRiscv64 : public DfxRegs {
103800b99b8Sopenharmony_cipublic:
104800b99b8Sopenharmony_ci    DfxRegsRiscv64() = default;
105800b99b8Sopenharmony_ci    ~DfxRegsRiscv64() = default;
106800b99b8Sopenharmony_ci    void SetFromUcontext(const ucontext_t& context) override;
107800b99b8Sopenharmony_ci    void SetFromFpMiniRegs(const uintptr_t* regs, const size_t size) override;
108800b99b8Sopenharmony_ci    void SetFromQutMiniRegs(const uintptr_t* regs, const size_t size) override;
109800b99b8Sopenharmony_ci    std::string PrintRegs() const override;
110800b99b8Sopenharmony_ci    bool SetPcFromReturnAddress(std::shared_ptr<DfxMemory> memory) override;
111800b99b8Sopenharmony_ci    bool StepIfSignalFrame(uintptr_t pc, std::shared_ptr<DfxMemory> memory) override;
112800b99b8Sopenharmony_ci};
113800b99b8Sopenharmony_ci#endif
114800b99b8Sopenharmony_ci
115800b99b8Sopenharmony_ci#if defined(__x86_64__)
116800b99b8Sopenharmony_ciclass DfxRegsX86_64 : public DfxRegs {
117800b99b8Sopenharmony_cipublic:
118800b99b8Sopenharmony_ci    DfxRegsX86_64() = default;
119800b99b8Sopenharmony_ci    ~DfxRegsX86_64() = default;
120800b99b8Sopenharmony_ci    void SetFromUcontext(const ucontext_t& context) override;
121800b99b8Sopenharmony_ci    void SetFromFpMiniRegs(const uintptr_t* regs, const size_t size) override;
122800b99b8Sopenharmony_ci    void SetFromQutMiniRegs(const uintptr_t* regs, const size_t size) override;
123800b99b8Sopenharmony_ci    std::string PrintRegs() const override;
124800b99b8Sopenharmony_ci    bool SetPcFromReturnAddress(std::shared_ptr<DfxMemory> memory) override;
125800b99b8Sopenharmony_ci    bool StepIfSignalFrame(uintptr_t pc, std::shared_ptr<DfxMemory> memory) override;
126800b99b8Sopenharmony_ci};
127800b99b8Sopenharmony_ci#endif
128800b99b8Sopenharmony_ci} // namespace HiviewDFX
129800b99b8Sopenharmony_ci} // namespace OHOS
130800b99b8Sopenharmony_ci#endif
131