blob: a9fb859eb300aab63dea201c959277554938ee15 [file] [log] [blame]
Christopher Ferris09385e72017-04-05 13:25:04 -07001/*
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>
Florian Mayer3f1f2e02018-10-23 15:56:28 +010022#include <string.h>
Christopher Ferris09385e72017-04-05 13:25:04 -070023#include <sys/mman.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include <android-base/unique_fd.h>
Yabin Cui3841acc2018-05-10 17:19:12 -070028#include <procinfo/process_map.h>
Christopher Ferris09385e72017-04-05 13:25:04 -070029
Yabin Cuid5b22c52018-02-22 17:11:31 -080030#include <algorithm>
Christopher Ferris60521c72017-08-18 15:10:53 -070031#include <cctype>
Christopher Ferris09385e72017-04-05 13:25:04 -070032#include <memory>
33#include <string>
34#include <vector>
35
Christopher Ferrisd226a512017-07-14 10:37:19 -070036#include <unwindstack/Elf.h>
37#include <unwindstack/Maps.h>
38#include <unwindstack/Memory.h>
39
40namespace unwindstack {
Christopher Ferris09385e72017-04-05 13:25:04 -070041
42MapInfo* 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;
Christopher Ferrisbe788d82017-11-27 14:50:38 -080050 MapInfo* cur = maps_[index];
Christopher Ferris09385e72017-04-05 13:25:04 -070051 if (pc >= cur->start && pc < cur->end) {
52 return cur;
53 } else if (pc < cur->start) {
54 last = index;
55 } else {
56 first = index + 1;
57 }
58 }
59 return nullptr;
60}
61
Christopher Ferris09385e72017-04-05 13:25:04 -070062bool Maps::Parse() {
Yabin Cui3841acc2018-05-10 17:19:12 -070063 return android::procinfo::ReadMapFile(
64 GetMapsFile(),
65 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
66 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
67 if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
68 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
69 }
Christopher Ferris9d5712c2018-10-01 21:01:09 -070070 maps_.push_back(new MapInfo(this, start, end, pgoff, flags, name));
Yabin Cui3841acc2018-05-10 17:19:12 -070071 });
Christopher Ferris09385e72017-04-05 13:25:04 -070072}
73
Christopher Ferrise7b66242017-12-15 11:17:45 -080074void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
75 const std::string& name, uint64_t load_bias) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -070076 MapInfo* map_info = new MapInfo(this, start, end, offset, flags, name);
Christopher Ferrise7b66242017-12-15 11:17:45 -080077 map_info->load_bias = load_bias;
78 maps_.push_back(map_info);
79}
80
Yabin Cuid5b22c52018-02-22 17:11:31 -080081void Maps::Sort() {
82 std::sort(maps_.begin(), maps_.end(),
83 [](const MapInfo* a, const MapInfo* b) { return a->start < b->start; });
84}
85
Christopher Ferris09385e72017-04-05 13:25:04 -070086Maps::~Maps() {
87 for (auto& map : maps_) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080088 delete map;
Christopher Ferris09385e72017-04-05 13:25:04 -070089 }
90}
91
92bool BufferMaps::Parse() {
Yabin Cui3841acc2018-05-10 17:19:12 -070093 std::string content(buffer_);
94 return android::procinfo::ReadMapFileContent(
95 &content[0],
96 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
97 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
98 if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
99 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
100 }
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700101 maps_.push_back(new MapInfo(this, start, end, pgoff, flags, name));
Yabin Cui3841acc2018-05-10 17:19:12 -0700102 });
Christopher Ferris09385e72017-04-05 13:25:04 -0700103}
104
105const std::string RemoteMaps::GetMapsFile() const {
106 return "/proc/" + std::to_string(pid_) + "/maps";
107}
108
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700109const std::string LocalUpdatableMaps::GetMapsFile() const {
110 return "/proc/self/maps";
111}
112
113bool LocalUpdatableMaps::Reparse() {
114 // New maps will be added at the end without deleting the old ones.
115 size_t last_map_idx = maps_.size();
116 if (!Parse()) {
117 // Delete any maps added by the Parse call.
118 for (size_t i = last_map_idx; i < maps_.size(); i++) {
119 delete maps_[i];
120 }
121 maps_.resize(last_map_idx);
122 return false;
123 }
124
125 size_t total_entries = maps_.size();
126 size_t search_map_idx = 0;
127 for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) {
128 MapInfo* new_map_info = maps_[new_map_idx];
129 uint64_t start = new_map_info->start;
130 uint64_t end = new_map_info->end;
131 uint64_t flags = new_map_info->flags;
132 std::string* name = &new_map_info->name;
133 for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) {
134 MapInfo* info = maps_[old_map_idx];
135 if (start == info->start && end == info->end && flags == info->flags && *name == info->name) {
136 // No need to check
137 search_map_idx = old_map_idx + 1;
138 delete new_map_info;
139 maps_[new_map_idx] = nullptr;
140 total_entries--;
141 break;
142 } else if (info->start > start) {
143 // Stop, there isn't going to be a match.
144 search_map_idx = old_map_idx;
145 break;
146 }
147
148 // Never delete these maps, they may be in use. The assumption is
149 // that there will only every be a handfull of these so waiting
150 // to destroy them is not too expensive.
151 saved_maps_.push_back(info);
152 maps_[old_map_idx] = nullptr;
153 total_entries--;
154 }
155 if (search_map_idx >= last_map_idx) {
156 break;
157 }
158 }
159
160 // Now move out any of the maps that never were found.
161 for (size_t i = search_map_idx; i < last_map_idx; i++) {
162 saved_maps_.push_back(maps_[i]);
163 maps_[i] = nullptr;
164 total_entries--;
165 }
166
167 // Sort all of the values such that the nullptrs wind up at the end, then
168 // resize them away.
169 std::sort(maps_.begin(), maps_.end(), [](const auto* a, const auto* b) {
170 if (a == nullptr) {
171 return false;
172 } else if (b == nullptr) {
173 return true;
174 }
175 return a->start < b->start;
176 });
177 maps_.resize(total_entries);
178
179 return true;
180}
181
182LocalUpdatableMaps::~LocalUpdatableMaps() {
183 for (auto map_info : saved_maps_) {
184 delete map_info;
185 }
186}
187
Christopher Ferrisd226a512017-07-14 10:37:19 -0700188} // namespace unwindstack