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 #include <errno.h>
18 #include <fcntl.h>
19 #include <inttypes.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/mman.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include <android-base/unique_fd.h>
28 #include <procinfo/process_map.h>
29
30 #include <algorithm>
31 #include <cctype>
32 #include <memory>
33 #include <string>
34 #include <vector>
35
36 #include <unwindstack/Elf.h>
37 #include <unwindstack/Maps.h>
38 #include <unwindstack/Memory.h>
39
40 namespace unwindstack {
41
Find(uint64_t pc)42 MapInfo* Maps::Find(uint64_t pc) {
43 if (maps_.empty()) {
44 return nullptr;
45 }
46 size_t first = 0;
47 size_t last = maps_.size();
48 while (first < last) {
49 size_t index = (first + last) / 2;
50 const auto& cur = maps_[index];
51 if (pc >= cur->start && pc < cur->end) {
52 return cur.get();
53 } else if (pc < cur->start) {
54 last = index;
55 } else {
56 first = index + 1;
57 }
58 }
59 return nullptr;
60 }
61
Parse()62 bool Maps::Parse() {
63 MapInfo* prev_map = nullptr;
64 MapInfo* prev_real_map = nullptr;
65 return android::procinfo::ReadMapFile(
66 GetMapsFile(),
67 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) {
68 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
69 if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
70 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
71 }
72 maps_.emplace_back(new MapInfo(prev_map, prev_real_map, start, end, pgoff, flags, name));
73 prev_map = maps_.back().get();
74 if (!prev_map->IsBlank()) {
75 prev_real_map = prev_map;
76 }
77 });
78 }
79
Add(uint64_t start,uint64_t end,uint64_t offset,uint64_t flags,const std::string & name,uint64_t load_bias)80 void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
81 const std::string& name, uint64_t load_bias) {
82 MapInfo* prev_map = maps_.empty() ? nullptr : maps_.back().get();
83 MapInfo* prev_real_map = prev_map;
84 while (prev_real_map != nullptr && prev_real_map->IsBlank()) {
85 prev_real_map = prev_real_map->prev_map;
86 }
87
88 auto map_info =
89 std::make_unique<MapInfo>(prev_map, prev_real_map, start, end, offset, flags, name);
90 map_info->load_bias = load_bias;
91 maps_.emplace_back(std::move(map_info));
92 }
93
Sort()94 void Maps::Sort() {
95 std::sort(maps_.begin(), maps_.end(),
96 [](const std::unique_ptr<MapInfo>& a, const std::unique_ptr<MapInfo>& b) {
97 return a->start < b->start; });
98
99 // Set the prev_map values on the info objects.
100 MapInfo* prev_map = nullptr;
101 MapInfo* prev_real_map = nullptr;
102 for (const auto& map_info : maps_) {
103 map_info->prev_map = prev_map;
104 map_info->prev_real_map = prev_real_map;
105 prev_map = map_info.get();
106 if (!prev_map->IsBlank()) {
107 prev_real_map = prev_map;
108 }
109 }
110 }
111
Parse()112 bool BufferMaps::Parse() {
113 std::string content(buffer_);
114 MapInfo* prev_map = nullptr;
115 MapInfo* prev_real_map = nullptr;
116 return android::procinfo::ReadMapFileContent(
117 &content[0],
118 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) {
119 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
120 if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
121 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
122 }
123 maps_.emplace_back(new MapInfo(prev_map, prev_real_map, start, end, pgoff, flags, name));
124 prev_map = maps_.back().get();
125 if (!prev_map->IsBlank()) {
126 prev_real_map = prev_map;
127 }
128 });
129 }
130
GetMapsFile() const131 const std::string RemoteMaps::GetMapsFile() const {
132 return "/proc/" + std::to_string(pid_) + "/maps";
133 }
134
GetMapsFile() const135 const std::string LocalUpdatableMaps::GetMapsFile() const {
136 return "/proc/self/maps";
137 }
138
Reparse()139 bool LocalUpdatableMaps::Reparse() {
140 // New maps will be added at the end without deleting the old ones.
141 size_t last_map_idx = maps_.size();
142 if (!Parse()) {
143 maps_.resize(last_map_idx);
144 return false;
145 }
146
147 size_t total_entries = maps_.size();
148 size_t search_map_idx = 0;
149 for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) {
150 auto& new_map_info = maps_[new_map_idx];
151 uint64_t start = new_map_info->start;
152 uint64_t end = new_map_info->end;
153 uint64_t flags = new_map_info->flags;
154 std::string* name = &new_map_info->name;
155 for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) {
156 auto& info = maps_[old_map_idx];
157 if (start == info->start && end == info->end && flags == info->flags && *name == info->name) {
158 // No need to check
159 search_map_idx = old_map_idx + 1;
160 if (new_map_idx + 1 < maps_.size()) {
161 maps_[new_map_idx + 1]->prev_map = info.get();
162 maps_[new_map_idx + 1]->prev_real_map =
163 info->IsBlank() ? info->prev_real_map : info.get();
164 }
165 maps_[new_map_idx] = nullptr;
166 total_entries--;
167 break;
168 } else if (info->start > start) {
169 // Stop, there isn't going to be a match.
170 search_map_idx = old_map_idx;
171 break;
172 }
173
174 // Never delete these maps, they may be in use. The assumption is
175 // that there will only every be a handful of these so waiting
176 // to destroy them is not too expensive.
177 saved_maps_.emplace_back(std::move(info));
178 search_map_idx = old_map_idx + 1;
179 maps_[old_map_idx] = nullptr;
180 total_entries--;
181 }
182 if (search_map_idx >= last_map_idx) {
183 break;
184 }
185 }
186
187 // Now move out any of the maps that never were found.
188 for (size_t i = search_map_idx; i < last_map_idx; i++) {
189 saved_maps_.emplace_back(std::move(maps_[i]));
190 maps_[i] = nullptr;
191 total_entries--;
192 }
193
194 // Sort all of the values such that the nullptrs wind up at the end, then
195 // resize them away.
196 std::sort(maps_.begin(), maps_.end(), [](const auto& a, const auto& b) {
197 if (a == nullptr) {
198 return false;
199 } else if (b == nullptr) {
200 return true;
201 }
202 return a->start < b->start;
203 });
204 maps_.resize(total_entries);
205
206 return true;
207 }
208
209 } // namespace unwindstack
210