1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 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_MEMORY_H
16800b99b8Sopenharmony_ci#define DFX_MEMORY_H
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <atomic>
19800b99b8Sopenharmony_ci#include <cstdint>
20800b99b8Sopenharmony_ci#include <string>
21800b99b8Sopenharmony_ci#include <unordered_map>
22800b99b8Sopenharmony_ci#include "dfx_accessors.h"
23800b99b8Sopenharmony_ci#include "unwind_context.h"
24800b99b8Sopenharmony_ci
25800b99b8Sopenharmony_cinamespace OHOS {
26800b99b8Sopenharmony_cinamespace HiviewDFX {
27800b99b8Sopenharmony_ciclass DfxMemory {
28800b99b8Sopenharmony_cipublic:
29800b99b8Sopenharmony_ci    DfxMemory() = default;
30800b99b8Sopenharmony_ci    explicit DfxMemory(std::shared_ptr<DfxAccessors> acc) : acc_(acc) {}
31800b99b8Sopenharmony_ci    virtual ~DfxMemory() = default;
32800b99b8Sopenharmony_ci
33800b99b8Sopenharmony_ci    void SetCtx(void* ctx) { ctx_ = ctx; }
34800b99b8Sopenharmony_ci    void SetAlign(bool alignAddr, int alignBytes)
35800b99b8Sopenharmony_ci    {
36800b99b8Sopenharmony_ci        alignAddr_ = alignAddr;
37800b99b8Sopenharmony_ci        alignBytes_ = alignBytes;
38800b99b8Sopenharmony_ci    }
39800b99b8Sopenharmony_ci
40800b99b8Sopenharmony_ci    bool ReadReg(int regIdx, uintptr_t *val);
41800b99b8Sopenharmony_ci    bool ReadMem(uintptr_t addr, uintptr_t *val);
42800b99b8Sopenharmony_ci
43800b99b8Sopenharmony_ci    virtual size_t Read(uintptr_t& addr, void* val, size_t size, bool incre = false);
44800b99b8Sopenharmony_ci    virtual bool ReadU8(uintptr_t& addr, uint8_t *val, bool incre = false);
45800b99b8Sopenharmony_ci    virtual bool ReadS8(uintptr_t& addr, int8_t *val, bool incre = false)
46800b99b8Sopenharmony_ci    {
47800b99b8Sopenharmony_ci        uint8_t valp = 0;
48800b99b8Sopenharmony_ci        bool ret = ReadU8(addr, &valp, incre);
49800b99b8Sopenharmony_ci        *val = static_cast<int8_t>(valp);
50800b99b8Sopenharmony_ci        return ret;
51800b99b8Sopenharmony_ci    }
52800b99b8Sopenharmony_ci    virtual bool ReadU16(uintptr_t& addr, uint16_t *val, bool incre = false);
53800b99b8Sopenharmony_ci    virtual bool ReadS16(uintptr_t& addr, int16_t *val, bool incre = false)
54800b99b8Sopenharmony_ci    {
55800b99b8Sopenharmony_ci        uint16_t valp = 0;
56800b99b8Sopenharmony_ci        bool ret = ReadU16(addr, &valp, incre);
57800b99b8Sopenharmony_ci        *val = static_cast<int16_t>(valp);
58800b99b8Sopenharmony_ci        return ret;
59800b99b8Sopenharmony_ci    }
60800b99b8Sopenharmony_ci    virtual bool ReadU32(uintptr_t& addr, uint32_t *val, bool incre = false);
61800b99b8Sopenharmony_ci    virtual bool ReadS32(uintptr_t& addr, int32_t *val, bool incre = false)
62800b99b8Sopenharmony_ci    {
63800b99b8Sopenharmony_ci        uint32_t valp = 0;
64800b99b8Sopenharmony_ci        bool ret = ReadU32(addr, &valp, incre);
65800b99b8Sopenharmony_ci        *val = static_cast<int32_t>(valp);
66800b99b8Sopenharmony_ci        return ret;
67800b99b8Sopenharmony_ci    }
68800b99b8Sopenharmony_ci    virtual bool ReadU64(uintptr_t& addr, uint64_t *val, bool incre = false);
69800b99b8Sopenharmony_ci    virtual bool ReadS64(uintptr_t& addr, int64_t *val, bool incre = false)
70800b99b8Sopenharmony_ci    {
71800b99b8Sopenharmony_ci        uint64_t valp = 0;
72800b99b8Sopenharmony_ci        bool ret = ReadU64(addr, &valp, incre);
73800b99b8Sopenharmony_ci        *val = static_cast<int64_t>(valp);
74800b99b8Sopenharmony_ci        return ret;
75800b99b8Sopenharmony_ci    }
76800b99b8Sopenharmony_ci    virtual bool ReadUptr(uintptr_t& addr, uintptr_t *val, bool incre = false);
77800b99b8Sopenharmony_ci    virtual bool ReadString(uintptr_t& addr, std::string* str, size_t maxSize, bool incre = false);
78800b99b8Sopenharmony_ci
79800b99b8Sopenharmony_ci    virtual bool ReadPrel31(uintptr_t& addr, uintptr_t *val);
80800b99b8Sopenharmony_ci
81800b99b8Sopenharmony_ci    virtual uint64_t ReadUleb128(uintptr_t& addr);
82800b99b8Sopenharmony_ci    virtual int64_t ReadSleb128(uintptr_t& addr);
83800b99b8Sopenharmony_ci    virtual void SetDataOffset(uintptr_t offset) { dataOffset_ = offset; }
84800b99b8Sopenharmony_ci    virtual void SetFuncOffset(uintptr_t offset) { funcOffset_ = offset; }
85800b99b8Sopenharmony_ci    virtual size_t GetEncodedSize(uint8_t encoding);
86800b99b8Sopenharmony_ci    virtual uintptr_t ReadEncodedValue(uintptr_t& addr, uint8_t encoding);
87800b99b8Sopenharmony_ci#if is_ohos && !is_mingw
88800b99b8Sopenharmony_ci    static size_t ReadProcMemByPid(const pid_t pid, const uint64_t addr, void* data, size_t size);
89800b99b8Sopenharmony_ci#endif
90800b99b8Sopenharmony_ciprivate:
91800b99b8Sopenharmony_ci    std::shared_ptr<DfxAccessors> acc_ = nullptr;
92800b99b8Sopenharmony_ci    void* ctx_ = nullptr;
93800b99b8Sopenharmony_ci    bool alignAddr_ = false;
94800b99b8Sopenharmony_ci    int alignBytes_ = 0;
95800b99b8Sopenharmony_ci    uintptr_t dataOffset_ = 0;
96800b99b8Sopenharmony_ci    uintptr_t funcOffset_ = 0;
97800b99b8Sopenharmony_ci};
98800b99b8Sopenharmony_ci} // namespace HiviewDFX
99800b99b8Sopenharmony_ci} // namespace OHOS
100800b99b8Sopenharmony_ci#endif
101