-
Notifications
You must be signed in to change notification settings - Fork 28
/
Maps.cpp
254 lines (223 loc) · 5.79 KB
/
Maps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <fcntl.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include "unistdfix.h"
#include "getline.h"
#include <android-base/unique_fd.h>
#include <algorithm>
#include <cctype>
#include <memory>
#include <string>
#include <vector>
#include <unwindstack/Elf.h>
#include <unwindstack/Maps.h>
#include <unwindstack/Memory.h>
namespace unwindstack {
MapInfo* Maps::Find(uint64_t pc) {
if (maps_.empty()) {
return nullptr;
}
size_t first = 0;
size_t last = maps_.size();
while (first < last) {
size_t index = (first + last) / 2;
MapInfo* cur = maps_[index];
if (pc >= cur->start && pc < cur->end) {
return cur;
} else if (pc < cur->start) {
last = index;
} else {
first = index + 1;
}
}
return nullptr;
}
// Assumes that line does not end in '\n'.
static MapInfo* InternalParseLine(const char* line) {
// Do not use a sscanf implementation since it is not performant.
// Example linux /proc/<pid>/maps lines:
// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so
char* str;
const char* old_str = line;
uint64_t start = strtoull(old_str, &str, 16);
if (old_str == str || *str++ != '-') {
return nullptr;
}
old_str = str;
uint64_t end = strtoull(old_str, &str, 16);
if (old_str == str || !std::isspace(*str++)) {
return nullptr;
}
while (std::isspace(*str)) {
str++;
}
// Parse permissions data.
if (*str == '\0') {
return nullptr;
}
uint16_t flags = 0;
if (*str == 'r') {
flags |= PROT_READ;
} else if (*str != '-') {
return nullptr;
}
str++;
if (*str == 'w') {
flags |= PROT_WRITE;
} else if (*str != '-') {
return nullptr;
}
str++;
if (*str == 'x') {
flags |= PROT_EXEC;
} else if (*str != '-') {
return nullptr;
}
str++;
if (*str != 'p' && *str != 's') {
return nullptr;
}
str++;
if (!std::isspace(*str++)) {
return nullptr;
}
old_str = str;
uint64_t offset = strtoull(old_str, &str, 16);
if (old_str == str || !std::isspace(*str)) {
return nullptr;
}
// Ignore the 00:00 values.
old_str = str;
(void)strtoull(old_str, &str, 16);
if (old_str == str || *str++ != ':') {
return nullptr;
}
if (std::isspace(*str)) {
return nullptr;
}
// Skip the inode.
old_str = str;
(void)strtoull(str, &str, 16);
if (old_str == str || !std::isspace(*str++)) {
return nullptr;
}
// Skip decimal digit.
old_str = str;
(void)strtoull(old_str, &str, 10);
if (old_str == str || (!std::isspace(*str) && *str != '\0')) {
return nullptr;
}
while (std::isspace(*str)) {
str++;
}
if (*str == '\0') {
return new MapInfo(start, end, offset, flags, "");
}
// Save the name data.
std::string name(str);
// Mark a device map in /dev/ and not in /dev/ashmem/ specially.
if (name.substr(0, 5) == "/dev/" && name.substr(5, 7) != "ashmem/") {
flags |= MAPS_FLAGS_DEVICE_MAP;
}
return new MapInfo(start, end, offset, flags, name);
}
bool Maps::Parse() {
int fd = open(GetMapsFile().c_str(), O_RDONLY | O_CLOEXEC);
if (fd == -1) {
return false;
}
bool return_value = true;
char buffer[2048];
size_t leftover = 0;
while (true) {
ssize_t bytes = read(fd, &buffer[leftover], 2048 - leftover);
if (bytes == -1) {
return_value = false;
break;
}
if (bytes == 0) {
break;
}
bytes += leftover;
char* line = buffer;
while (bytes > 0) {
char* newline = static_cast<char*>(memchr(line, '\n', bytes));
if (newline == nullptr) {
memmove(buffer, line, bytes);
break;
}
*newline = '\0';
MapInfo* map_info = InternalParseLine(line);
if (map_info == nullptr) {
return_value = false;
break;
}
maps_.push_back(map_info);
bytes -= newline - line + 1;
line = newline + 1;
}
leftover = bytes;
}
close(fd);
return return_value;
}
void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
const std::string& name, uint64_t load_bias) {
MapInfo* map_info = new MapInfo(start, end, offset, flags, name);
map_info->load_bias = load_bias;
maps_.push_back(map_info);
}
void Maps::Sort() {
std::sort(maps_.begin(), maps_.end(),
[](const MapInfo* a, const MapInfo* b) { return a->start < b->start; });
}
Maps::~Maps() {
for (auto& map : maps_) {
delete map;
}
}
bool BufferMaps::Parse() {
const char* start_of_line = buffer_;
do {
std::string line;
const char* end_of_line = strchr(start_of_line, '\n');
if (end_of_line == nullptr) {
line = start_of_line;
} else {
line = std::string(start_of_line, end_of_line - start_of_line);
end_of_line++;
}
MapInfo* map_info = InternalParseLine(line.c_str());
if (map_info == nullptr) {
return false;
}
maps_.push_back(map_info);
start_of_line = end_of_line;
} while (start_of_line != nullptr && *start_of_line != '\0');
return true;
}
const std::string RemoteMaps::GetMapsFile() const {
return "/proc/" + std::to_string(pid_) + "/maps";
}
} // namespace unwindstack