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
16800b99b8Sopenharmony_ci#ifndef DFX_ACCESSORS_H
17800b99b8Sopenharmony_ci#define DFX_ACCESSORS_H
18800b99b8Sopenharmony_ci
19800b99b8Sopenharmony_ci#include <atomic>
20800b99b8Sopenharmony_ci#include <cstdint>
21800b99b8Sopenharmony_ci#include <memory>
22800b99b8Sopenharmony_ci#include <mutex>
23800b99b8Sopenharmony_ci#include <string>
24800b99b8Sopenharmony_ci#include "dfx_define.h"
25800b99b8Sopenharmony_ci#include "unwind_context.h"
26800b99b8Sopenharmony_ci
27800b99b8Sopenharmony_ci#if is_mingw
28800b99b8Sopenharmony_ci#ifndef UNWIND_BYTE_ORDER
29800b99b8Sopenharmony_ci#define UNWIND_BYTE_ORDER -1 // unknown
30800b99b8Sopenharmony_ci#endif
31800b99b8Sopenharmony_ci#endif
32800b99b8Sopenharmony_ci
33800b99b8Sopenharmony_cinamespace OHOS {
34800b99b8Sopenharmony_cinamespace HiviewDFX {
35800b99b8Sopenharmony_ciclass DfxMap;
36800b99b8Sopenharmony_ci
37800b99b8Sopenharmony_ciclass DfxAccessors {
38800b99b8Sopenharmony_cipublic:
39800b99b8Sopenharmony_ci    DfxAccessors(int bigEndian = UNWIND_BYTE_ORDER) : bigEndian_(bigEndian) {}
40800b99b8Sopenharmony_ci    virtual ~DfxAccessors() = default;
41800b99b8Sopenharmony_ci    static bool GetMapByPcAndCtx(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg);
42800b99b8Sopenharmony_ci
43800b99b8Sopenharmony_ci    virtual int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) = 0;
44800b99b8Sopenharmony_ci    virtual int AccessReg(int regIdx, uintptr_t *val, void *arg) = 0;
45800b99b8Sopenharmony_ci    virtual int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) = 0;
46800b99b8Sopenharmony_ci    virtual int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) = 0;
47800b99b8Sopenharmony_ci
48800b99b8Sopenharmony_ci    int bigEndian_ = UNWIND_BYTE_ORDER;
49800b99b8Sopenharmony_ci};
50800b99b8Sopenharmony_ci
51800b99b8Sopenharmony_ciclass DfxAccessorsLocal : public DfxAccessors {
52800b99b8Sopenharmony_cipublic:
53800b99b8Sopenharmony_ci    DfxAccessorsLocal() = default;
54800b99b8Sopenharmony_ci    ~DfxAccessorsLocal();
55800b99b8Sopenharmony_ci    bool IsValidFrame(uintptr_t addr, uintptr_t stackBottom, uintptr_t stackTop);
56800b99b8Sopenharmony_ci
57800b99b8Sopenharmony_ci    int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) override;
58800b99b8Sopenharmony_ci    int AccessReg(int regIdx, uintptr_t *val, void *arg) override;
59800b99b8Sopenharmony_ci    int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) override;
60800b99b8Sopenharmony_ci    int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) override;
61800b99b8Sopenharmony_ciprivate:
62800b99b8Sopenharmony_ci    DfxAccessorsLocal(const DfxAccessorsLocal&) = delete;
63800b99b8Sopenharmony_ci    DfxAccessorsLocal& operator= (const DfxAccessorsLocal&) = delete;
64800b99b8Sopenharmony_ci    bool CreatePipe();
65800b99b8Sopenharmony_ci
66800b99b8Sopenharmony_ci    std::mutex mutex_;
67800b99b8Sopenharmony_ci    int32_t pfd_[PIPE_NUM_SZ] = {-1, -1};
68800b99b8Sopenharmony_ci    bool initPipe_ = false;
69800b99b8Sopenharmony_ci};
70800b99b8Sopenharmony_ci
71800b99b8Sopenharmony_ciclass DfxAccessorsRemote : public DfxAccessors {
72800b99b8Sopenharmony_cipublic:
73800b99b8Sopenharmony_ci    DfxAccessorsRemote() = default;
74800b99b8Sopenharmony_ci    virtual ~DfxAccessorsRemote() = default;
75800b99b8Sopenharmony_ci
76800b99b8Sopenharmony_ci    int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) override;
77800b99b8Sopenharmony_ci    int AccessReg(int regIdx, uintptr_t *val, void *arg) override;
78800b99b8Sopenharmony_ci    int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) override;
79800b99b8Sopenharmony_ci    int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) override;
80800b99b8Sopenharmony_ci};
81800b99b8Sopenharmony_ci
82800b99b8Sopenharmony_ciclass DfxAccessorsCustomize : public DfxAccessors {
83800b99b8Sopenharmony_cipublic:
84800b99b8Sopenharmony_ci    DfxAccessorsCustomize(std::shared_ptr<UnwindAccessors> accessors) : accessors_(accessors) {}
85800b99b8Sopenharmony_ci    virtual ~DfxAccessorsCustomize() = default;
86800b99b8Sopenharmony_ci
87800b99b8Sopenharmony_ci    int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) override;
88800b99b8Sopenharmony_ci    int AccessReg(int regIdx, uintptr_t *val, void *arg) override;
89800b99b8Sopenharmony_ci    int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) override;
90800b99b8Sopenharmony_ci    int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) override;
91800b99b8Sopenharmony_ciprivate:
92800b99b8Sopenharmony_ci    std::shared_ptr<UnwindAccessors> accessors_;
93800b99b8Sopenharmony_ci};
94800b99b8Sopenharmony_ci} // namespace HiviewDFX
95800b99b8Sopenharmony_ci} // namespace OHOS
96800b99b8Sopenharmony_ci#endif
97