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 #ifndef _LIBUNWINDSTACK_MEMORY_H
18 #define _LIBUNWINDSTACK_MEMORY_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 #include <memory>
25 #include <string>
26 
27 namespace unwindstack {
28 
29 class MemoryCacheBase;
30 
31 class Memory {
32  public:
33   Memory() = default;
34   virtual ~Memory() = default;
35 
36   static std::shared_ptr<Memory> CreateProcessMemory(pid_t pid);
37   static std::shared_ptr<Memory> CreateProcessMemoryCached(pid_t pid);
38   static std::shared_ptr<Memory> CreateProcessMemoryThreadCached(pid_t pid);
39   static std::shared_ptr<Memory> CreateOfflineMemory(const uint8_t* data, uint64_t start,
40                                                      uint64_t end);
41   static std::unique_ptr<Memory> CreateFileMemory(const std::string& path, uint64_t offset,
42                                                   uint64_t size = UINT64_MAX);
43 
AsMemoryCacheBase()44   virtual MemoryCacheBase* AsMemoryCacheBase() { return nullptr; }
45 
46   virtual bool ReadString(uint64_t addr, std::string* dst, size_t max_read);
47 
Clear()48   virtual void Clear() {}
49 
50   // Get pointer to directly access the data for buffers that support it.
51   virtual uint8_t* GetPtr(size_t /*addr*/ = 0) { return nullptr; }
52 
53   virtual size_t Read(uint64_t addr, void* dst, size_t size) = 0;
ReadTag(uint64_t)54   virtual long ReadTag(uint64_t) { return -1; }
55 
56   bool ReadFully(uint64_t addr, void* dst, size_t size);
57 
Read32(uint64_t addr,uint32_t * dst)58   inline bool Read32(uint64_t addr, uint32_t* dst) {
59     return ReadFully(addr, dst, sizeof(uint32_t));
60   }
61 
Read64(uint64_t addr,uint64_t * dst)62   inline bool Read64(uint64_t addr, uint64_t* dst) {
63     return ReadFully(addr, dst, sizeof(uint64_t));
64   }
65 };
66 
67 }  // namespace unwindstack
68 
69 #endif  // _LIBUNWINDSTACK_MEMORY_H
70