blob: c5316a1d08abd7ee0743f86f49b37e039fbfe532 [file] [log] [blame]
Christopher Ferris723cf9b2017-01-19 20:08:48 -08001/*
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>
Christopher Ferris3958f802017-02-01 15:44:40 -080025#include <vector>
Christopher Ferris723cf9b2017-01-19 20:08:48 -080026
27class 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
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
41 inline bool Read32(uint64_t addr, uint32_t* dst) {
42 return Read(addr, dst, sizeof(uint32_t));
43 }
44
45 inline bool Read64(uint64_t addr, uint64_t* dst) {
46 return Read(addr, dst, sizeof(uint64_t));
47 }
48};
49
Christopher Ferris3958f802017-02-01 15:44:40 -080050class 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
59 void Resize(size_t size) { raw_.resize(size); }
60
61 uint64_t Size() { return raw_.size(); }
62
63 private:
64 std::vector<uint8_t> raw_;
65};
66
Christopher Ferris723cf9b2017-01-19 20:08:48 -080067class MemoryFileAtOffset : public Memory {
68 public:
69 MemoryFileAtOffset() = default;
70 virtual ~MemoryFileAtOffset();
71
Christopher Ferris3958f802017-02-01 15:44:40 -080072 bool Init(const std::string& file, uint64_t offset, uint64_t size = UINT64_MAX);
Christopher Ferris723cf9b2017-01-19 20:08:48 -080073
74 bool Read(uint64_t addr, void* dst, size_t size) override;
75
Christopher Ferris3958f802017-02-01 15:44:40 -080076 void Clear();
77
Christopher Ferris723cf9b2017-01-19 20:08:48 -080078 protected:
79 size_t size_ = 0;
80 size_t offset_ = 0;
81 uint8_t* data_ = nullptr;
82};
83
84class 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
97class MemoryRemote : public Memory {
98 public:
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
104 pid_t pid() { return pid_; }
105
106 private:
107 pid_t pid_;
108};
109
110class 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
118class MemoryRange : public Memory {
119 public:
120 MemoryRange(Memory* memory, uint64_t begin, uint64_t end)
121 : memory_(memory), begin_(begin), length_(end - begin_) {}
122 virtual ~MemoryRange() { delete memory_; }
123
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