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 Memory {
30  public:
31   Memory() = default;
32   virtual ~Memory() = default;
33 
34   static std::shared_ptr<Memory> CreateProcessMemory(pid_t pid);
35   static std::shared_ptr<Memory> CreateProcessMemoryCached(pid_t pid);
36   static std::shared_ptr<Memory> CreateOfflineMemory(const uint8_t* data, uint64_t start,
37                                                      uint64_t end);
38   static std::unique_ptr<Memory> CreateFileMemory(const std::string& path, uint64_t offset);
39 
40   virtual bool ReadString(uint64_t addr, std::string* string, uint64_t max_read = UINT64_MAX);
41 
Clear()42   virtual void Clear() {}
43 
44   virtual size_t Read(uint64_t addr, void* dst, size_t size) = 0;
45 
46   bool ReadFully(uint64_t addr, void* dst, size_t size);
47 
Read32(uint64_t addr,uint32_t * dst)48   inline bool Read32(uint64_t addr, uint32_t* dst) {
49     return ReadFully(addr, dst, sizeof(uint32_t));
50   }
51 
Read64(uint64_t addr,uint64_t * dst)52   inline bool Read64(uint64_t addr, uint64_t* dst) {
53     return ReadFully(addr, dst, sizeof(uint64_t));
54   }
55 };
56 
57 }  // namespace unwindstack
58 
59 #endif  // _LIBUNWINDSTACK_MEMORY_H
60