• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 DFX_MEMORY_H
16 #define DFX_MEMORY_H
17 
18 #include <atomic>
19 #include <cstdint>
20 #include <string>
21 #include <unordered_map>
22 #include "dfx_accessors.h"
23 #include "unwind_context.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 class DfxMemory {
28 public:
29     DfxMemory() = default;
DfxMemory(std::shared_ptr<DfxAccessors> acc)30     explicit DfxMemory(std::shared_ptr<DfxAccessors> acc) : acc_(acc) {}
31     virtual ~DfxMemory() = default;
32 
SetCtx(void * ctx)33     void SetCtx(void* ctx) { ctx_ = ctx; }
SetAlign(bool alignAddr,int alignBytes)34     void SetAlign(bool alignAddr, int alignBytes)
35     {
36         alignAddr_ = alignAddr;
37         alignBytes_ = alignBytes;
38     }
39 
40     bool ReadReg(int regIdx, uintptr_t *val);
41     bool ReadMem(uintptr_t addr, uintptr_t *val);
42 
43     virtual size_t Read(uintptr_t& addr, void* val, size_t size, bool incre = false);
44     virtual bool ReadU8(uintptr_t& addr, uint8_t *val, bool incre = false);
45     virtual bool ReadS8(uintptr_t& addr, int8_t *val, bool incre = false)
46     {
47         uint8_t valp = 0;
48         bool ret = ReadU8(addr, &valp, incre);
49         *val = static_cast<int8_t>(valp);
50         return ret;
51     }
52     virtual bool ReadU16(uintptr_t& addr, uint16_t *val, bool incre = false);
53     virtual bool ReadS16(uintptr_t& addr, int16_t *val, bool incre = false)
54     {
55         uint16_t valp = 0;
56         bool ret = ReadU16(addr, &valp, incre);
57         *val = static_cast<int16_t>(valp);
58         return ret;
59     }
60     virtual bool ReadU32(uintptr_t& addr, uint32_t *val, bool incre = false);
61     virtual bool ReadS32(uintptr_t& addr, int32_t *val, bool incre = false)
62     {
63         uint32_t valp = 0;
64         bool ret = ReadU32(addr, &valp, incre);
65         *val = static_cast<int32_t>(valp);
66         return ret;
67     }
68     virtual bool ReadU64(uintptr_t& addr, uint64_t *val, bool incre = false);
69     virtual bool ReadS64(uintptr_t& addr, int64_t *val, bool incre = false)
70     {
71         uint64_t valp = 0;
72         bool ret = ReadU64(addr, &valp, incre);
73         *val = static_cast<int64_t>(valp);
74         return ret;
75     }
76     virtual bool ReadUptr(uintptr_t& addr, uintptr_t *val, bool incre = false);
77     virtual bool ReadString(uintptr_t& addr, std::string* str, size_t maxSize, bool incre = false);
78 
79     virtual bool ReadPrel31(uintptr_t& addr, uintptr_t *val);
80 
81     virtual uint64_t ReadUleb128(uintptr_t& addr);
82     virtual int64_t ReadSleb128(uintptr_t& addr);
SetDataOffset(uintptr_t offset)83     virtual void SetDataOffset(uintptr_t offset) { dataOffset_ = offset; }
SetFuncOffset(uintptr_t offset)84     virtual void SetFuncOffset(uintptr_t offset) { funcOffset_ = offset; }
85     virtual size_t GetEncodedSize(uint8_t encoding);
86     virtual uintptr_t ReadEncodedValue(uintptr_t& addr, uint8_t encoding);
87 #if is_ohos && !is_mingw
88     static size_t ReadProcMemByPid(const pid_t pid, const uint64_t addr, void* data, size_t size);
89 #endif
90 private:
91     std::shared_ptr<DfxAccessors> acc_;
92     void* ctx_ = nullptr;
93     bool alignAddr_ = false;
94     int alignBytes_ = 0;
95     uintptr_t dataOffset_ = 0;
96     uintptr_t funcOffset_ = 0;
97 };
98 } // namespace HiviewDFX
99 } // namespace OHOS
100 #endif
101