• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   static std::shared_ptr<Memory> CreateOfflineMemory(const uint8_t* data, uint64_t start,
39                                                      uint64_t end);
40   static std::unique_ptr<Memory> CreateFileMemory(const std::string& path, uint64_t offset,
41                                                   uint64_t size = UINT64_MAX);
42 
AsMemoryCacheBase()43   virtual MemoryCacheBase* AsMemoryCacheBase() { return nullptr; }
44 
45   virtual bool ReadString(uint64_t addr, std::string* dst, size_t max_read);
46 
Clear()47   virtual void Clear() {}
48 
49   // Get pointer to directly access the data for buffers that support it.
50   virtual uint8_t* GetPtr(size_t /*addr*/ = 0) { return nullptr; }
51 
52   virtual size_t Read(uint64_t addr, void* dst, size_t size) = 0;
ReadTag(uint64_t)53   virtual long ReadTag(uint64_t) { return -1; }
54 
55   bool ReadFully(uint64_t addr, void* dst, size_t size);
56 
Read32(uint64_t addr,uint32_t * dst)57   inline bool Read32(uint64_t addr, uint32_t* dst) {
58     return ReadFully(addr, dst, sizeof(uint32_t));
59   }
60 
Read64(uint64_t addr,uint64_t * dst)61   inline bool Read64(uint64_t addr, uint64_t* dst) {
62     return ReadFully(addr, dst, sizeof(uint64_t));
63   }
64 };
65 
66 }  // namespace unwindstack
67