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; |
Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 50 | const auto& cur = maps_[index]; |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 51 | if (pc >= cur->start && pc < cur->end) { |
Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 52 | return cur.get(); |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 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(), |
Sandeep Patil | f31c709 | 2019-01-30 17:43:22 -0800 | [diff] [blame] | 65 | [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) { |
Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 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 | } |
Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 70 | maps_.emplace_back( |
| 71 | new MapInfo(maps_.empty() ? nullptr : maps_.back().get(), start, end, pgoff, |
| 72 | flags, name)); |
Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 73 | }); |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 76 | void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, |
| 77 | const std::string& name, uint64_t load_bias) { |
Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 78 | auto map_info = |
| 79 | std::make_unique<MapInfo>(maps_.empty() ? nullptr : maps_.back().get(), start, end, offset, |
| 80 | flags, name); |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 81 | map_info->load_bias = load_bias; |
Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 82 | maps_.emplace_back(std::move(map_info)); |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Yabin Cui | d5b22c5 | 2018-02-22 17:11:31 -0800 | [diff] [blame] | 85 | void Maps::Sort() { |
| 86 | std::sort(maps_.begin(), maps_.end(), |
Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 87 | [](const std::unique_ptr<MapInfo>& a, const std::unique_ptr<MapInfo>& b) { |
| 88 | return a->start < b->start; }); |
Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 89 | |
| 90 | // Set the prev_map values on the info objects. |
| 91 | MapInfo* prev_map = nullptr; |
Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 92 | for (const auto& map_info : maps_) { |
Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 93 | map_info->prev_map = prev_map; |
Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 94 | prev_map = map_info.get(); |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | bool BufferMaps::Parse() { |
Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 99 | std::string content(buffer_); |
| 100 | return android::procinfo::ReadMapFileContent( |
| 101 | &content[0], |
Sandeep Patil | f31c709 | 2019-01-30 17:43:22 -0800 | [diff] [blame] | 102 | [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) { |
Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 103 | // 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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 107 | maps_.emplace_back( |
| 108 | new MapInfo(maps_.empty() ? nullptr : maps_.back().get(), start, end, pgoff, |
| 109 | flags, name)); |
Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 110 | }); |
Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | const std::string RemoteMaps::GetMapsFile() const { |
| 114 | return "/proc/" + std::to_string(pid_) + "/maps"; |
| 115 | } |
| 116 | |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 117 | const std::string LocalUpdatableMaps::GetMapsFile() const { |
| 118 | return "/proc/self/maps"; |
| 119 | } |
| 120 | |
| 121 | bool 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 Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 125 | 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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 132 | auto& new_map_info = maps_[new_map_idx]; |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 133 | 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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 138 | auto& info = maps_[old_map_idx]; |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 139 | 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 Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 142 | 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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 154 | saved_maps_.emplace_back(std::move(info)); |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 155 | 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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 165 | saved_maps_.emplace_back(std::move(maps_[i])); |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 166 | 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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame^] | 172 | std::sort(maps_.begin(), maps_.end(), [](const auto& a, const auto& b) { |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 173 | 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 Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 185 | } // namespace unwindstack |