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 <string>
25 #include <vector>
26 
27 class Memory {
28  public:
29   Memory() = default;
30   virtual ~Memory() = default;
31 
32   virtual bool ReadString(uint64_t addr, std::string* string, uint64_t max_read = UINT64_MAX);
33 
34   virtual bool Read(uint64_t addr, void* dst, size_t size) = 0;
35 
Read(uint64_t addr,void * start,void * field,size_t size)36   inline bool Read(uint64_t addr, void* start, void* field, size_t size) {
37     return Read(addr + reinterpret_cast<uintptr_t>(field) - reinterpret_cast<uintptr_t>(start),
38                 field, size);
39   }
40 
Read32(uint64_t addr,uint32_t * dst)41   inline bool Read32(uint64_t addr, uint32_t* dst) {
42     return Read(addr, dst, sizeof(uint32_t));
43   }
44 
Read64(uint64_t addr,uint64_t * dst)45   inline bool Read64(uint64_t addr, uint64_t* dst) {
46     return Read(addr, dst, sizeof(uint64_t));
47   }
48 };
49 
50 class MemoryBuffer : public Memory {
51  public:
52   MemoryBuffer() = default;
53   virtual ~MemoryBuffer() = default;
54 
55   bool Read(uint64_t addr, void* dst, size_t size) override;
56 
57   uint8_t* GetPtr(size_t offset);
58 
Resize(size_t size)59   void Resize(size_t size) { raw_.resize(size); }
60 
Size()61   uint64_t Size() { return raw_.size(); }
62 
63  private:
64   std::vector<uint8_t> raw_;
65 };
66 
67 class MemoryFileAtOffset : public Memory {
68  public:
69   MemoryFileAtOffset() = default;
70   virtual ~MemoryFileAtOffset();
71 
72   bool Init(const std::string& file, uint64_t offset, uint64_t size = UINT64_MAX);
73 
74   bool Read(uint64_t addr, void* dst, size_t size) override;
75 
76   void Clear();
77 
78  protected:
79   size_t size_ = 0;
80   size_t offset_ = 0;
81   uint8_t* data_ = nullptr;
82 };
83 
84 class MemoryOffline : public MemoryFileAtOffset {
85  public:
86   MemoryOffline() = default;
87   virtual ~MemoryOffline() = default;
88 
89   bool Init(const std::string& file, uint64_t offset);
90 
91   bool Read(uint64_t addr, void* dst, size_t size) override;
92 
93  private:
94   uint64_t start_;
95 };
96 
97 class MemoryRemote : public Memory {
98  public:
MemoryRemote(pid_t pid)99   MemoryRemote(pid_t pid) : pid_(pid) {}
100   virtual ~MemoryRemote() = default;
101 
102   bool Read(uint64_t addr, void* dst, size_t size) override;
103 
pid()104   pid_t pid() { return pid_; }
105 
106  private:
107   pid_t pid_;
108 };
109 
110 class MemoryLocal : public Memory {
111  public:
112   MemoryLocal() = default;
113   virtual ~MemoryLocal() = default;
114 
115   bool Read(uint64_t addr, void* dst, size_t size) override;
116 };
117 
118 class MemoryRange : public Memory {
119  public:
MemoryRange(Memory * memory,uint64_t begin,uint64_t end)120   MemoryRange(Memory* memory, uint64_t begin, uint64_t end)
121       : memory_(memory), begin_(begin), length_(end - begin_) {}
~MemoryRange()122   virtual ~MemoryRange() { delete memory_; }
123 
Read(uint64_t addr,void * dst,size_t size)124   inline bool Read(uint64_t addr, void* dst, size_t size) override {
125     if (addr + size <= length_) {
126       return memory_->Read(addr + begin_, dst, size);
127     }
128     return false;
129   }
130 
131  private:
132   Memory* memory_;
133   uint64_t begin_;
134   uint64_t length_;
135 };
136 
137 #endif  // _LIBUNWINDSTACK_MEMORY_H
138