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* dst, size_t max_read);
41 
Clear()42   virtual void Clear() {}
43 
IsLocal()44   virtual bool IsLocal() const { return false; }
45 
46   virtual size_t Read(uint64_t addr, void* dst, size_t size) = 0;
ReadTag(uint64_t)47   virtual long ReadTag(uint64_t) { return -1; }
48 
49   bool ReadFully(uint64_t addr, void* dst, size_t size);
50 
Read32(uint64_t addr,uint32_t * dst)51   inline bool Read32(uint64_t addr, uint32_t* dst) {
52     return ReadFully(addr, dst, sizeof(uint32_t));
53   }
54 
Read64(uint64_t addr,uint64_t * dst)55   inline bool Read64(uint64_t addr, uint64_t* dst) {
56     return ReadFully(addr, dst, sizeof(uint64_t));
57   }
58 };
59 
60 }  // namespace unwindstack
61 
62 #endif  // _LIBUNWINDSTACK_MEMORY_H
63