blob: e676a5a97b3b0d6f9676ebfcad4e31309e2b31d8 [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>
22#include <sys/mman.h>
23#include <sys/types.h>
24#include <unistd.h>
25
26#include <android-base/unique_fd.h>
Yabin Cui3841acc2018-05-10 17:19:12 -070027#include <procinfo/process_map.h>
Christopher Ferris09385e72017-04-05 13:25:04 -070028
Yabin Cuid5b22c52018-02-22 17:11:31 -080029#include <algorithm>
Christopher Ferris60521c72017-08-18 15:10:53 -070030#include <cctype>
Christopher Ferris09385e72017-04-05 13:25:04 -070031#include <memory>
32#include <string>
33#include <vector>
34
Christopher Ferrisd226a512017-07-14 10:37:19 -070035#include <unwindstack/Elf.h>
36#include <unwindstack/Maps.h>
37#include <unwindstack/Memory.h>
38
39namespace unwindstack {
Christopher Ferris09385e72017-04-05 13:25:04 -070040
41MapInfo* Maps::Find(uint64_t pc) {
42 if (maps_.empty()) {
43 return nullptr;
44 }
45 size_t first = 0;
46 size_t last = maps_.size();
47 while (first < last) {
48 size_t index = (first + last) / 2;
Christopher Ferrisbe788d82017-11-27 14:50:38 -080049 MapInfo* cur = maps_[index];
Christopher Ferris09385e72017-04-05 13:25:04 -070050 if (pc >= cur->start && pc < cur->end) {
51 return cur;
52 } else if (pc < cur->start) {
53 last = index;
54 } else {
55 first = index + 1;
56 }
57 }
58 return nullptr;
59}
60
Christopher Ferris09385e72017-04-05 13:25:04 -070061bool Maps::Parse() {
Yabin Cui3841acc2018-05-10 17:19:12 -070062 return android::procinfo::ReadMapFile(
63 GetMapsFile(),
64 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
65 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
66 if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
67 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
68 }
69 maps_.push_back(new MapInfo(start, end, pgoff, flags, name));
70 });
Christopher Ferris09385e72017-04-05 13:25:04 -070071}
72
Christopher Ferrise7b66242017-12-15 11:17:45 -080073void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
74 const std::string& name, uint64_t load_bias) {
75 MapInfo* map_info = new MapInfo(start, end, offset, flags, name);
76 map_info->load_bias = load_bias;
77 maps_.push_back(map_info);
78}
79
Yabin Cuid5b22c52018-02-22 17:11:31 -080080void Maps::Sort() {
81 std::sort(maps_.begin(), maps_.end(),
82 [](const MapInfo* a, const MapInfo* b) { return a->start < b->start; });
83}
84
Christopher Ferris09385e72017-04-05 13:25:04 -070085Maps::~Maps() {
86 for (auto& map : maps_) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080087 delete map;
Christopher Ferris09385e72017-04-05 13:25:04 -070088 }
89}
90
91bool BufferMaps::Parse() {
Yabin Cui3841acc2018-05-10 17:19:12 -070092 std::string content(buffer_);
93 return android::procinfo::ReadMapFileContent(
94 &content[0],
95 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
96 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
97 if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
98 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
99 }
100 maps_.push_back(new MapInfo(start, end, pgoff, flags, name));
101 });
Christopher Ferris09385e72017-04-05 13:25:04 -0700102}
103
104const std::string RemoteMaps::GetMapsFile() const {
105 return "/proc/" + std::to_string(pid_) + "/maps";
106}
107
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700108const std::string LocalUpdatableMaps::GetMapsFile() const {
109 return "/proc/self/maps";
110}
111
112bool LocalUpdatableMaps::Reparse() {
113 // New maps will be added at the end without deleting the old ones.
114 size_t last_map_idx = maps_.size();
115 if (!Parse()) {
116 // Delete any maps added by the Parse call.
117 for (size_t i = last_map_idx; i < maps_.size(); i++) {
118 delete maps_[i];
119 }
120 maps_.resize(last_map_idx);
121 return false;
122 }
123
124 size_t total_entries = maps_.size();
125 size_t search_map_idx = 0;
126 for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) {
127 MapInfo* new_map_info = maps_[new_map_idx];
128 uint64_t start = new_map_info->start;
129 uint64_t end = new_map_info->end;
130 uint64_t flags = new_map_info->flags;
131 std::string* name = &new_map_info->name;
132 for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) {
133 MapInfo* info = maps_[old_map_idx];
134 if (start == info->start && end == info->end && flags == info->flags && *name == info->name) {
135 // No need to check
136 search_map_idx = old_map_idx + 1;
137 delete new_map_info;
138 maps_[new_map_idx] = nullptr;
139 total_entries--;
140 break;
141 } else if (info->start > start) {
142 // Stop, there isn't going to be a match.
143 search_map_idx = old_map_idx;
144 break;
145 }
146
147 // Never delete these maps, they may be in use. The assumption is
148 // that there will only every be a handfull of these so waiting
149 // to destroy them is not too expensive.
150 saved_maps_.push_back(info);
151 maps_[old_map_idx] = nullptr;
152 total_entries--;
153 }
154 if (search_map_idx >= last_map_idx) {
155 break;
156 }
157 }
158
159 // Now move out any of the maps that never were found.
160 for (size_t i = search_map_idx; i < last_map_idx; i++) {
161 saved_maps_.push_back(maps_[i]);
162 maps_[i] = nullptr;
163 total_entries--;
164 }
165
166 // Sort all of the values such that the nullptrs wind up at the end, then
167 // resize them away.
168 std::sort(maps_.begin(), maps_.end(), [](const auto* a, const auto* b) {
169 if (a == nullptr) {
170 return false;
171 } else if (b == nullptr) {
172 return true;
173 }
174 return a->start < b->start;
175 });
176 maps_.resize(total_entries);
177
178 return true;
179}
180
181LocalUpdatableMaps::~LocalUpdatableMaps() {
182 for (auto map_info : saved_maps_) {
183 delete map_info;
184 }
185}
186
Christopher Ferrisd226a512017-07-14 10:37:19 -0700187} // namespace unwindstack