1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <stdint.h> 20 #include <sys/types.h> 21 #include <unistd.h> 22 23 #include <memory> 24 #include <string> 25 26 namespace unwindstack { 27 28 class MemoryCacheBase; 29 30 class Memory { 31 public: 32 Memory() = default; 33 virtual ~Memory() = default; 34 35 static std::shared_ptr<Memory> CreateProcessMemory(pid_t pid); 36 static std::shared_ptr<Memory> CreateProcessMemoryCached(pid_t pid); 37 static std::shared_ptr<Memory> CreateProcessMemoryThreadCached(pid_t pid); 38 // This should only be used for performance. Using this could result 39 // in crashes if used to try and read stack data. 40 static std::shared_ptr<Memory> CreateProcessMemoryLocalUnsafe(); 41 42 static std::shared_ptr<Memory> CreateOfflineMemory(const uint8_t* data, uint64_t start, 43 uint64_t end); 44 static std::shared_ptr<Memory> CreateFileMemory(const std::string& path, uint64_t offset, 45 uint64_t size = UINT64_MAX); 46 AsMemoryCacheBase()47 virtual MemoryCacheBase* AsMemoryCacheBase() { return nullptr; } 48 49 virtual bool ReadString(uint64_t addr, std::string* dst, size_t max_read); 50 Clear()51 virtual void Clear() {} 52 53 // Get pointer to directly access the data for buffers that support it. 54 virtual uint8_t* GetPtr(size_t /*addr*/ = 0) { return nullptr; } 55 56 virtual size_t Read(uint64_t addr, void* dst, size_t size) = 0; ReadTag(uint64_t)57 virtual long ReadTag(uint64_t) { return -1; } 58 59 bool ReadFully(uint64_t addr, void* dst, size_t size); 60 Read32(uint64_t addr,uint32_t * dst)61 inline bool Read32(uint64_t addr, uint32_t* dst) { 62 return ReadFully(addr, dst, sizeof(uint32_t)); 63 } 64 Read64(uint64_t addr,uint64_t * dst)65 inline bool Read64(uint64_t addr, uint64_t* dst) { 66 return ReadFully(addr, dst, sizeof(uint64_t)); 67 } 68 }; 69 70 } // namespace unwindstack 71