blob: 5da73e46432c8e265015deeddc1e56453b023436 [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() {
Yabin Cui3841acc2018-05-10 17:19:12 -070063 return android::procinfo::ReadMapFile(
64 GetMapsFile(),
Sandeep Patilf31c7092019-01-30 17:43:22 -080065 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) {
Yabin Cui3841acc2018-05-10 17:19:12 -070066 // 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 }
Florian Mayer3d67d342019-02-27 18:00:37 +000070 maps_.emplace_back(
71 new MapInfo(maps_.empty() ? nullptr : maps_.back().get(), start, end, pgoff,
72 flags, name));
Yabin Cui3841acc2018-05-10 17:19:12 -070073 });
Christopher Ferris09385e72017-04-05 13:25:04 -070074}
75
Christopher Ferrise7b66242017-12-15 11:17:45 -080076void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
77 const std::string& name, uint64_t load_bias) {
Florian Mayer3d67d342019-02-27 18:00:37 +000078 auto map_info =
79 std::make_unique<MapInfo>(maps_.empty() ? nullptr : maps_.back().get(), start, end, offset,
80 flags, name);
Christopher Ferrise7b66242017-12-15 11:17:45 -080081 map_info->load_bias = load_bias;
Florian Mayer3d67d342019-02-27 18:00:37 +000082 maps_.emplace_back(std::move(map_info));
Christopher Ferrise7b66242017-12-15 11:17:45 -080083}
84
Yabin Cuid5b22c52018-02-22 17:11:31 -080085void Maps::Sort() {
86 std::sort(maps_.begin(), maps_.end(),
Florian Mayer3d67d342019-02-27 18:00:37 +000087 [](const std::unique_ptr<MapInfo>& a, const std::unique_ptr<MapInfo>& b) {
88 return a->start < b->start; });
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080089
90 // Set the prev_map values on the info objects.
91 MapInfo* prev_map = nullptr;
Florian Mayer3d67d342019-02-27 18:00:37 +000092 for (const auto& map_info : maps_) {
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080093 map_info->prev_map = prev_map;
Florian Mayer3d67d342019-02-27 18:00:37 +000094 prev_map = map_info.get();
Christopher Ferris09385e72017-04-05 13:25:04 -070095 }
96}
97
98bool BufferMaps::Parse() {
Yabin Cui3841acc2018-05-10 17:19:12 -070099 std::string content(buffer_);
100 return android::procinfo::ReadMapFileContent(
101 &content[0],
Sandeep Patilf31c7092019-01-30 17:43:22 -0800102 [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) {
Yabin Cui3841acc2018-05-10 17:19:12 -0700103 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
104 if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
105 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
106 }
Florian Mayer3d67d342019-02-27 18:00:37 +0000107 maps_.emplace_back(
108 new MapInfo(maps_.empty() ? nullptr : maps_.back().get(), start, end, pgoff,
109 flags, name));
Yabin Cui3841acc2018-05-10 17:19:12 -0700110 });
Christopher Ferris09385e72017-04-05 13:25:04 -0700111}
112
113const std::string RemoteMaps::GetMapsFile() const {
114 return "/proc/" + std::to_string(pid_) + "/maps";
115}
116
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700117const std::string LocalUpdatableMaps::GetMapsFile() const {
118 return "/proc/self/maps";
119}
120
121bool LocalUpdatableMaps::Reparse() {
122 // New maps will be added at the end without deleting the old ones.
123 size_t last_map_idx = maps_.size();
124 if (!Parse()) {
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700125 maps_.resize(last_map_idx);
126 return false;
127 }
128
129 size_t total_entries = maps_.size();
130 size_t search_map_idx = 0;
131 for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) {
Florian Mayer3d67d342019-02-27 18:00:37 +0000132 auto& new_map_info = maps_[new_map_idx];
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700133 uint64_t start = new_map_info->start;
134 uint64_t end = new_map_info->end;
135 uint64_t flags = new_map_info->flags;
136 std::string* name = &new_map_info->name;
137 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 +0000138 auto& info = maps_[old_map_idx];
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700139 if (start == info->start && end == info->end && flags == info->flags && *name == info->name) {
140 // No need to check
141 search_map_idx = old_map_idx + 1;
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700142 maps_[new_map_idx] = nullptr;
143 total_entries--;
144 break;
145 } else if (info->start > start) {
146 // Stop, there isn't going to be a match.
147 search_map_idx = old_map_idx;
148 break;
149 }
150
151 // Never delete these maps, they may be in use. The assumption is
152 // that there will only every be a handfull of these so waiting
153 // to destroy them is not too expensive.
Florian Mayer3d67d342019-02-27 18:00:37 +0000154 saved_maps_.emplace_back(std::move(info));
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700155 maps_[old_map_idx] = nullptr;
156 total_entries--;
157 }
158 if (search_map_idx >= last_map_idx) {
159 break;
160 }
161 }
162
163 // Now move out any of the maps that never were found.
164 for (size_t i = search_map_idx; i < last_map_idx; i++) {
Florian Mayer3d67d342019-02-27 18:00:37 +0000165 saved_maps_.emplace_back(std::move(maps_[i]));
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700166 maps_[i] = nullptr;
167 total_entries--;
168 }
169
170 // Sort all of the values such that the nullptrs wind up at the end, then
171 // resize them away.
Florian Mayer3d67d342019-02-27 18:00:37 +0000172 std::sort(maps_.begin(), maps_.end(), [](const auto& a, const auto& b) {
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700173 if (a == nullptr) {
174 return false;
175 } else if (b == nullptr) {
176 return true;
177 }
178 return a->start < b->start;
179 });
180 maps_.resize(total_entries);
181
182 return true;
183}
184
Christopher Ferrisd226a512017-07-14 10:37:19 -0700185} // namespace unwindstack