1 /* 2 * Copyright (c) 2023-2025 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; 30 explicit DfxMemory(const UnwindType& unwindType, std::shared_ptr<UnwindAccessors> accessors = nullptr); 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 45 template <typename T> 46 bool Read(uintptr_t& addr, T* val, bool incre = false) 47 { 48 if (Read(addr, val, sizeof(T), incre) == sizeof(T)) { 49 return true; 50 } 51 return false; 52 } 53 54 template <typename T> 55 inline T ReadValue(uintptr_t& addr, bool incre = false) 56 { 57 T tmp = 0; 58 Read<T>(addr, &tmp, incre); 59 return tmp; 60 } 61 62 virtual bool ReadString(uintptr_t& addr, std::string* str, size_t maxSize, bool incre = false); 63 64 virtual bool ReadPrel31(uintptr_t& addr, uintptr_t* val); 65 66 virtual uint64_t ReadUleb128(uintptr_t& addr); 67 virtual int64_t ReadSleb128(uintptr_t& addr); SetDataOffset(uintptr_t offset)68 virtual void SetDataOffset(uintptr_t offset) { dataOffset_ = offset; } SetFuncOffset(uintptr_t offset)69 virtual void SetFuncOffset(uintptr_t offset) { funcOffset_ = offset; } 70 virtual size_t GetEncodedSize(uint8_t encoding); 71 virtual uintptr_t ReadEncodedValue(uintptr_t& addr, uint8_t encoding); 72 void ReadFormatEncodedValue(uintptr_t& addr, uintptr_t& val, uint8_t formatEncoding); 73 #if is_ohos && !is_mingw 74 static size_t ReadProcMemByPid(const pid_t pid, const uint64_t addr, void* data, size_t size); 75 #endif 76 int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti) const; 77 int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map) const; 78 private: 79 std::shared_ptr<DfxAccessors> acc_ = nullptr; 80 void* ctx_ = nullptr; 81 bool alignAddr_ = false; 82 int alignBytes_ = 0; 83 uintptr_t dataOffset_ = 0; 84 uintptr_t funcOffset_ = 0; 85 }; 86 } // namespace HiviewDFX 87 } // namespace OHOS 88 #endif 89