Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Mayer | 3f1f2e0 | 2018-10-23 15:56:28 +0100 | [diff] [blame] | 22 | #include <string.h> |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 23 | #include <sys/mman.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <android-base/unique_fd.h> |
Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 28 | #include <procinfo/process_map.h> |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 29 | |
Yabin Cui | d5b22c5 | 2018-02-22 17:11:31 -0800 | [diff] [blame] | 30 | #include <algorithm> |
Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 31 | #include <cctype> |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 32 | #include <memory> |
| 33 | #include <string> |
| 34 | #include <vector> |
| 35 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 36 | #include <unwindstack/Elf.h> |
| 37 | #include <unwindstack/Maps.h> |
| 38 | #include <unwindstack/Memory.h> |
| 39 | |
| 40 | namespace unwindstack { |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 41 | |
| 42 | MapInfo* 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 Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 50 | MapInfo* cur = maps_[index]; |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 51 | 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 Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 62 | bool Maps::Parse() { |
Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 63 | 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 Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame^] | 70 | maps_.push_back( |
| 71 | new MapInfo(maps_.empty() ? nullptr : maps_.back(), start, end, pgoff, flags, name)); |
Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 72 | }); |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 75 | void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, |
| 76 | const std::string& name, uint64_t load_bias) { |
Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame^] | 77 | MapInfo* map_info = |
| 78 | new MapInfo(maps_.empty() ? nullptr : maps_.back(), start, end, offset, flags, name); |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 79 | map_info->load_bias = load_bias; |
| 80 | maps_.push_back(map_info); |
| 81 | } |
| 82 | |
Yabin Cui | d5b22c5 | 2018-02-22 17:11:31 -0800 | [diff] [blame] | 83 | void Maps::Sort() { |
| 84 | std::sort(maps_.begin(), maps_.end(), |
| 85 | [](const MapInfo* a, const MapInfo* b) { return a->start < b->start; }); |
Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame^] | 86 | |
| 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 Cui | d5b22c5 | 2018-02-22 17:11:31 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 95 | Maps::~Maps() { |
| 96 | for (auto& map : maps_) { |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 97 | delete map; |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | bool BufferMaps::Parse() { |
Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 102 | 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 Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame^] | 110 | maps_.push_back( |
| 111 | new MapInfo(maps_.empty() ? nullptr : maps_.back(), start, end, pgoff, flags, name)); |
Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 112 | }); |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | const std::string RemoteMaps::GetMapsFile() const { |
| 116 | return "/proc/" + std::to_string(pid_) + "/maps"; |
| 117 | } |
| 118 | |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 119 | const std::string LocalUpdatableMaps::GetMapsFile() const { |
| 120 | return "/proc/self/maps"; |
| 121 | } |
| 122 | |
| 123 | bool 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 | |
| 192 | LocalUpdatableMaps::~LocalUpdatableMaps() { |
| 193 | for (auto map_info : saved_maps_) { |
| 194 | delete map_info; |
| 195 | } |
| 196 | } |
| 197 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 198 | } // namespace unwindstack |