2017-04-05 22:25:04 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2018-10-23 16:56:28 +02:00
|
|
|
#include <string.h>
|
2017-04-05 22:25:04 +02:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <android-base/unique_fd.h>
|
2018-05-11 02:19:12 +02:00
|
|
|
#include <procinfo/process_map.h>
|
2017-04-05 22:25:04 +02:00
|
|
|
|
2018-02-23 02:11:31 +01:00
|
|
|
#include <algorithm>
|
2017-08-19 00:10:53 +02:00
|
|
|
#include <cctype>
|
2017-04-05 22:25:04 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-07-14 19:37:19 +02:00
|
|
|
#include <unwindstack/Elf.h>
|
|
|
|
#include <unwindstack/Maps.h>
|
|
|
|
#include <unwindstack/Memory.h>
|
|
|
|
|
|
|
|
namespace unwindstack {
|
2017-04-05 22:25:04 +02:00
|
|
|
|
|
|
|
MapInfo* Maps::Find(uint64_t pc) {
|
|
|
|
if (maps_.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
size_t first = 0;
|
|
|
|
size_t last = maps_.size();
|
|
|
|
while (first < last) {
|
|
|
|
size_t index = (first + last) / 2;
|
2017-11-27 23:50:38 +01:00
|
|
|
MapInfo* cur = maps_[index];
|
2017-04-05 22:25:04 +02:00
|
|
|
if (pc >= cur->start && pc < cur->end) {
|
|
|
|
return cur;
|
|
|
|
} else if (pc < cur->start) {
|
|
|
|
last = index;
|
|
|
|
} else {
|
|
|
|
first = index + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Maps::Parse() {
|
2018-05-11 02:19:12 +02:00
|
|
|
return android::procinfo::ReadMapFile(
|
|
|
|
GetMapsFile(),
|
|
|
|
[&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
|
|
|
|
// Mark a device map in /dev/ and not in /dev/ashmem/ specially.
|
|
|
|
if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
|
|
|
|
flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
|
|
|
|
}
|
2018-12-14 01:08:50 +01:00
|
|
|
maps_.push_back(
|
|
|
|
new MapInfo(maps_.empty() ? nullptr : maps_.back(), start, end, pgoff, flags, name));
|
2018-05-11 02:19:12 +02:00
|
|
|
});
|
2017-04-05 22:25:04 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 20:17:45 +01:00
|
|
|
void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
|
|
|
|
const std::string& name, uint64_t load_bias) {
|
2018-12-14 01:08:50 +01:00
|
|
|
MapInfo* map_info =
|
|
|
|
new MapInfo(maps_.empty() ? nullptr : maps_.back(), start, end, offset, flags, name);
|
2017-12-15 20:17:45 +01:00
|
|
|
map_info->load_bias = load_bias;
|
|
|
|
maps_.push_back(map_info);
|
|
|
|
}
|
|
|
|
|
2018-02-23 02:11:31 +01:00
|
|
|
void Maps::Sort() {
|
|
|
|
std::sort(maps_.begin(), maps_.end(),
|
|
|
|
[](const MapInfo* a, const MapInfo* b) { return a->start < b->start; });
|
2018-12-14 01:08:50 +01:00
|
|
|
|
|
|
|
// Set the prev_map values on the info objects.
|
|
|
|
MapInfo* prev_map = nullptr;
|
|
|
|
for (MapInfo* map_info : maps_) {
|
|
|
|
map_info->prev_map = prev_map;
|
|
|
|
prev_map = map_info;
|
|
|
|
}
|
2018-02-23 02:11:31 +01:00
|
|
|
}
|
|
|
|
|
2017-04-05 22:25:04 +02:00
|
|
|
Maps::~Maps() {
|
|
|
|
for (auto& map : maps_) {
|
2017-11-27 23:50:38 +01:00
|
|
|
delete map;
|
2017-04-05 22:25:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BufferMaps::Parse() {
|
2018-05-11 02:19:12 +02:00
|
|
|
std::string content(buffer_);
|
|
|
|
return android::procinfo::ReadMapFileContent(
|
|
|
|
&content[0],
|
|
|
|
[&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
|
|
|
|
// Mark a device map in /dev/ and not in /dev/ashmem/ specially.
|
|
|
|
if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
|
|
|
|
flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
|
|
|
|
}
|
2018-12-14 01:08:50 +01:00
|
|
|
maps_.push_back(
|
|
|
|
new MapInfo(maps_.empty() ? nullptr : maps_.back(), start, end, pgoff, flags, name));
|
2018-05-11 02:19:12 +02:00
|
|
|
});
|
2017-04-05 22:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string RemoteMaps::GetMapsFile() const {
|
|
|
|
return "/proc/" + std::to_string(pid_) + "/maps";
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:15:00 +02:00
|
|
|
const std::string LocalUpdatableMaps::GetMapsFile() const {
|
|
|
|
return "/proc/self/maps";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalUpdatableMaps::Reparse() {
|
|
|
|
// New maps will be added at the end without deleting the old ones.
|
|
|
|
size_t last_map_idx = maps_.size();
|
|
|
|
if (!Parse()) {
|
|
|
|
// Delete any maps added by the Parse call.
|
|
|
|
for (size_t i = last_map_idx; i < maps_.size(); i++) {
|
|
|
|
delete maps_[i];
|
|
|
|
}
|
|
|
|
maps_.resize(last_map_idx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t total_entries = maps_.size();
|
|
|
|
size_t search_map_idx = 0;
|
|
|
|
for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) {
|
|
|
|
MapInfo* new_map_info = maps_[new_map_idx];
|
|
|
|
uint64_t start = new_map_info->start;
|
|
|
|
uint64_t end = new_map_info->end;
|
|
|
|
uint64_t flags = new_map_info->flags;
|
|
|
|
std::string* name = &new_map_info->name;
|
|
|
|
for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) {
|
|
|
|
MapInfo* info = maps_[old_map_idx];
|
|
|
|
if (start == info->start && end == info->end && flags == info->flags && *name == info->name) {
|
|
|
|
// No need to check
|
|
|
|
search_map_idx = old_map_idx + 1;
|
|
|
|
delete new_map_info;
|
|
|
|
maps_[new_map_idx] = nullptr;
|
|
|
|
total_entries--;
|
|
|
|
break;
|
|
|
|
} else if (info->start > start) {
|
|
|
|
// Stop, there isn't going to be a match.
|
|
|
|
search_map_idx = old_map_idx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never delete these maps, they may be in use. The assumption is
|
|
|
|
// that there will only every be a handfull of these so waiting
|
|
|
|
// to destroy them is not too expensive.
|
|
|
|
saved_maps_.push_back(info);
|
|
|
|
maps_[old_map_idx] = nullptr;
|
|
|
|
total_entries--;
|
|
|
|
}
|
|
|
|
if (search_map_idx >= last_map_idx) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now move out any of the maps that never were found.
|
|
|
|
for (size_t i = search_map_idx; i < last_map_idx; i++) {
|
|
|
|
saved_maps_.push_back(maps_[i]);
|
|
|
|
maps_[i] = nullptr;
|
|
|
|
total_entries--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort all of the values such that the nullptrs wind up at the end, then
|
|
|
|
// resize them away.
|
|
|
|
std::sort(maps_.begin(), maps_.end(), [](const auto* a, const auto* b) {
|
|
|
|
if (a == nullptr) {
|
|
|
|
return false;
|
|
|
|
} else if (b == nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return a->start < b->start;
|
|
|
|
});
|
|
|
|
maps_.resize(total_entries);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalUpdatableMaps::~LocalUpdatableMaps() {
|
|
|
|
for (auto map_info : saved_maps_) {
|
|
|
|
delete map_info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 19:37:19 +02:00
|
|
|
} // namespace unwindstack
|