blob: 670d9046f657af5a1c7ab98165224209a0558279 [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;
Florian Mayer3d67d342019-02-27 18:00:37 +000050 const auto& cur = maps_[index];
Christopher Ferris09385e72017-04-05 13:25:04 -070051 if (pc >= cur->start && pc < cur->end) {
Florian Mayer3d67d342019-02-27 18:00:37 +000052 return cur.get();
Christopher Ferris09385e72017-04-05 13:25:04 -070053 } 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() {
Christopher Ferris0f40a052020-01-22 12:17:06 -080063 MapInfo* prev_map = nullptr;
64 MapInfo* prev_real_map = nullptr;
Yabin Cui3841acc2018-05-10 17:19:12 -070065 return android::procinfo::ReadMapFile(
66 GetMapsFile(),
Sandeep Patilf31c7092019-01-30 17:43:22 -080067 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) {
Yabin Cui3841acc2018-05-10 17:19:12 -070068 // 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 }
Christopher Ferris0f40a052020-01-22 12:17:06 -080072 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 }
Yabin Cui3841acc2018-05-10 17:19:12 -070077 });
Christopher Ferris09385e72017-04-05 13:25:04 -070078}
79
Christopher Ferrise7b66242017-12-15 11:17:45 -080080void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
81 const std::string& name, uint64_t load_bias) {
Christopher Ferris0f40a052020-01-22 12:17:06 -080082 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
Florian Mayer3d67d342019-02-27 18:00:37 +000088 auto map_info =
Christopher Ferris0f40a052020-01-22 12:17:06 -080089 std::make_unique<MapInfo>(prev_map, prev_real_map, start, end, offset, flags, name);
Christopher Ferrise7b66242017-12-15 11:17:45 -080090 map_info->load_bias = load_bias;
Florian Mayer3d67d342019-02-27 18:00:37 +000091 maps_.emplace_back(std::move(map_info));
Christopher Ferrise7b66242017-12-15 11:17:45 -080092}
93
Yabin Cuid5b22c52018-02-22 17:11:31 -080094void Maps::Sort() {
95 std::sort(maps_.begin(), maps_.end(),
Florian Mayer3d67d342019-02-27 18:00:37 +000096 [](const std::unique_ptr<MapInfo>& a, const std::unique_ptr<MapInfo>& b) {
97 return a->start < b->start; });
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080098
99 // Set the prev_map values on the info objects.
100 MapInfo* prev_map = nullptr;
Christopher Ferris0f40a052020-01-22 12:17:06 -0800101 MapInfo* prev_real_map = nullptr;
Florian Mayer3d67d342019-02-27 18:00:37 +0000102 for (const auto& map_info : maps_) {
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800103 map_info->prev_map = prev_map;
Christopher Ferris0f40a052020-01-22 12:17:06 -0800104 map_info->prev_real_map = prev_real_map;
Florian Mayer3d67d342019-02-27 18:00:37 +0000105 prev_map = map_info.get();
Christopher Ferris0f40a052020-01-22 12:17:06 -0800106 if (!prev_map->IsBlank()) {
107 prev_real_map = prev_map;
108 }
Christopher Ferris09385e72017-04-05 13:25:04 -0700109 }
110}
111
112bool BufferMaps::Parse() {
Yabin Cui3841acc2018-05-10 17:19:12 -0700113 std::string content(buffer_);
Christopher Ferris0f40a052020-01-22 12:17:06 -0800114 MapInfo* prev_map = nullptr;
115 MapInfo* prev_real_map = nullptr;
Yabin Cui3841acc2018-05-10 17:19:12 -0700116 return android::procinfo::ReadMapFileContent(
117 &content[0],
Sandeep Patilf31c7092019-01-30 17:43:22 -0800118 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) {
Yabin Cui3841acc2018-05-10 17:19:12 -0700119 // 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 }
Christopher Ferris0f40a052020-01-22 12:17:06 -0800123 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 }
Yabin Cui3841acc2018-05-10 17:19:12 -0700128 });
Christopher Ferris09385e72017-04-05 13:25:04 -0700129}
130
131const std::string RemoteMaps::GetMapsFile() const {
132 return "/proc/" + std::to_string(pid_) + "/maps";
133}
134
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700135const std::string LocalUpdatableMaps::GetMapsFile() const {
136 return "/proc/self/maps";
137}
138
139bool 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()) {
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700143 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++) {
Florian Mayer3d67d342019-02-27 18:00:37 +0000150 auto& new_map_info = maps_[new_map_idx];
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700151 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++) {
Florian Mayer3d67d342019-02-27 18:00:37 +0000156 auto& info = maps_[old_map_idx];
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700157 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;
Josh Gao8a243112019-11-14 17:33:12 -0800160 if (new_map_idx + 1 < maps_.size()) {
161 maps_[new_map_idx + 1]->prev_map = info.get();
Sim Suna7a194b2020-04-16 04:02:59 -0700162 maps_[new_map_idx + 1]->prev_real_map =
163 info->IsBlank() ? info->prev_real_map : info.get();
Josh Gao8a243112019-11-14 17:33:12 -0800164 }
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700165 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
David Srbeckyf1932fd2019-09-16 22:36:55 +0100175 // that there will only every be a handful of these so waiting
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700176 // to destroy them is not too expensive.
Florian Mayer3d67d342019-02-27 18:00:37 +0000177 saved_maps_.emplace_back(std::move(info));
David Srbeckyf1932fd2019-09-16 22:36:55 +0100178 search_map_idx = old_map_idx + 1;
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700179 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++) {
Florian Mayer3d67d342019-02-27 18:00:37 +0000189 saved_maps_.emplace_back(std::move(maps_[i]));
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700190 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.
Florian Mayer3d67d342019-02-27 18:00:37 +0000196 std::sort(maps_.begin(), maps_.end(), [](const auto& a, const auto& b) {
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700197 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
Christopher Ferrisd226a512017-07-14 10:37:19 -0700209} // namespace unwindstack