blob: c90e383095cf5c7598ec915866fca8d45da641b7 [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 Ferrisa09c4a62018-12-13 16:08:50 -080070 maps_.push_back(
71 new MapInfo(maps_.empty() ? nullptr : maps_.back(), start, end, pgoff, flags, name));
Yabin Cui3841acc2018-05-10 17:19:12 -070072 });
Christopher Ferris09385e72017-04-05 13:25:04 -070073}
74
Christopher Ferrise7b66242017-12-15 11:17:45 -080075void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
76 const std::string& name, uint64_t load_bias) {
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080077 MapInfo* map_info =
78 new MapInfo(maps_.empty() ? nullptr : maps_.back(), start, end, offset, flags, name);
Christopher Ferrise7b66242017-12-15 11:17:45 -080079 map_info->load_bias = load_bias;
80 maps_.push_back(map_info);
81}
82
Yabin Cuid5b22c52018-02-22 17:11:31 -080083void Maps::Sort() {
84 std::sort(maps_.begin(), maps_.end(),
85 [](const MapInfo* a, const MapInfo* b) { return a->start < b->start; });
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080086
87 // Set the prev_map values on the info objects.
88 MapInfo* prev_map = nullptr;
89 for (MapInfo* map_info : maps_) {
90 map_info->prev_map = prev_map;
91 prev_map = map_info;
92 }
Yabin Cuid5b22c52018-02-22 17:11:31 -080093}
94
Christopher Ferris09385e72017-04-05 13:25:04 -070095Maps::~Maps() {
96 for (auto& map : maps_) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080097 delete map;
Christopher Ferris09385e72017-04-05 13:25:04 -070098 }
99}
100
101bool BufferMaps::Parse() {
Yabin Cui3841acc2018-05-10 17:19:12 -0700102 std::string content(buffer_);
103 return android::procinfo::ReadMapFileContent(
104 &content[0],
105 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
106 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
107 if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
108 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
109 }
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800110 maps_.push_back(
111 new MapInfo(maps_.empty() ? nullptr : maps_.back(), start, end, pgoff, flags, name));
Yabin Cui3841acc2018-05-10 17:19:12 -0700112 });
Christopher Ferris09385e72017-04-05 13:25:04 -0700113}
114
115const std::string RemoteMaps::GetMapsFile() const {
116 return "/proc/" + std::to_string(pid_) + "/maps";
117}
118
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700119const std::string LocalUpdatableMaps::GetMapsFile() const {
120 return "/proc/self/maps";
121}
122
123bool LocalUpdatableMaps::Reparse() {
124 // New maps will be added at the end without deleting the old ones.
125 size_t last_map_idx = maps_.size();
126 if (!Parse()) {
127 // Delete any maps added by the Parse call.
128 for (size_t i = last_map_idx; i < maps_.size(); i++) {
129 delete maps_[i];
130 }
131 maps_.resize(last_map_idx);
132 return false;
133 }
134
135 size_t total_entries = maps_.size();
136 size_t search_map_idx = 0;
137 for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) {
138 MapInfo* new_map_info = maps_[new_map_idx];
139 uint64_t start = new_map_info->start;
140 uint64_t end = new_map_info->end;
141 uint64_t flags = new_map_info->flags;
142 std::string* name = &new_map_info->name;
143 for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) {
144 MapInfo* info = maps_[old_map_idx];
145 if (start == info->start && end == info->end && flags == info->flags && *name == info->name) {
146 // No need to check
147 search_map_idx = old_map_idx + 1;
148 delete new_map_info;
149 maps_[new_map_idx] = nullptr;
150 total_entries--;
151 break;
152 } else if (info->start > start) {
153 // Stop, there isn't going to be a match.
154 search_map_idx = old_map_idx;
155 break;
156 }
157
158 // Never delete these maps, they may be in use. The assumption is
159 // that there will only every be a handfull of these so waiting
160 // to destroy them is not too expensive.
161 saved_maps_.push_back(info);
162 maps_[old_map_idx] = nullptr;
163 total_entries--;
164 }
165 if (search_map_idx >= last_map_idx) {
166 break;
167 }
168 }
169
170 // Now move out any of the maps that never were found.
171 for (size_t i = search_map_idx; i < last_map_idx; i++) {
172 saved_maps_.push_back(maps_[i]);
173 maps_[i] = nullptr;
174 total_entries--;
175 }
176
177 // Sort all of the values such that the nullptrs wind up at the end, then
178 // resize them away.
179 std::sort(maps_.begin(), maps_.end(), [](const auto* a, const auto* b) {
180 if (a == nullptr) {
181 return false;
182 } else if (b == nullptr) {
183 return true;
184 }
185 return a->start < b->start;
186 });
187 maps_.resize(total_entries);
188
189 return true;
190}
191
192LocalUpdatableMaps::~LocalUpdatableMaps() {
193 for (auto map_info : saved_maps_) {
194 delete map_info;
195 }
196}
197
Christopher Ferrisd226a512017-07-14 10:37:19 -0700198} // namespace unwindstack