| 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() { | 
| Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 63 |   MapInfo* prev_map = nullptr; | 
 | 64 |   MapInfo* prev_real_map = nullptr; | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 65 |   return android::procinfo::ReadMapFile( | 
 | 66 |       GetMapsFile(), | 
| Sandeep Patil | f31c709 | 2019-01-30 17:43:22 -0800 | [diff] [blame] | 67 |       [&](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] | 68 |         // 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 Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 72 |         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 Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 77 |       }); | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 78 | } | 
 | 79 |  | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 80 | void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, | 
 | 81 |                const std::string& name, uint64_t load_bias) { | 
| Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 82 |   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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 88 |   auto map_info = | 
| Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 89 |       std::make_unique<MapInfo>(prev_map, prev_real_map, start, end, offset, flags, name); | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 90 |   map_info->load_bias = load_bias; | 
| Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 91 |   maps_.emplace_back(std::move(map_info)); | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 92 | } | 
 | 93 |  | 
| Yabin Cui | d5b22c5 | 2018-02-22 17:11:31 -0800 | [diff] [blame] | 94 | void Maps::Sort() { | 
 | 95 |   std::sort(maps_.begin(), maps_.end(), | 
| Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 96 |             [](const std::unique_ptr<MapInfo>& a, const std::unique_ptr<MapInfo>& b) { | 
 | 97 |               return a->start < b->start; }); | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 98 |  | 
 | 99 |   // Set the prev_map values on the info objects. | 
 | 100 |   MapInfo* prev_map = nullptr; | 
| Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 101 |   MapInfo* prev_real_map = nullptr; | 
| Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 102 |   for (const auto& map_info : maps_) { | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 103 |     map_info->prev_map = prev_map; | 
| Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 104 |     map_info->prev_real_map = prev_real_map; | 
| Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 105 |     prev_map = map_info.get(); | 
| Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 106 |     if (!prev_map->IsBlank()) { | 
 | 107 |       prev_real_map = prev_map; | 
 | 108 |     } | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 109 |   } | 
 | 110 | } | 
 | 111 |  | 
 | 112 | bool BufferMaps::Parse() { | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 113 |   std::string content(buffer_); | 
| Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 114 |   MapInfo* prev_map = nullptr; | 
 | 115 |   MapInfo* prev_real_map = nullptr; | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 116 |   return android::procinfo::ReadMapFileContent( | 
 | 117 |       &content[0], | 
| Sandeep Patil | f31c709 | 2019-01-30 17:43:22 -0800 | [diff] [blame] | 118 |       [&](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] | 119 |         // 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 Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 123 |         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 Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 128 |       }); | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 129 | } | 
 | 130 |  | 
 | 131 | const std::string RemoteMaps::GetMapsFile() const { | 
 | 132 |   return "/proc/" + std::to_string(pid_) + "/maps"; | 
 | 133 | } | 
 | 134 |  | 
| Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 135 | const std::string LocalUpdatableMaps::GetMapsFile() const { | 
 | 136 |   return "/proc/self/maps"; | 
 | 137 | } | 
 | 138 |  | 
 | 139 | bool 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 Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 143 |     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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 150 |     auto& new_map_info = maps_[new_map_idx]; | 
| Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 151 |     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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 156 |       auto& info = maps_[old_map_idx]; | 
| Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 157 |       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 Gao | 8a24311 | 2019-11-14 17:33:12 -0800 | [diff] [blame] | 160 |         if (new_map_idx + 1 < maps_.size()) { | 
 | 161 |           maps_[new_map_idx + 1]->prev_map = info.get(); | 
| Sim Sun | a7a194b | 2020-04-16 04:02:59 -0700 | [diff] [blame] | 162 |           maps_[new_map_idx + 1]->prev_real_map = | 
 | 163 |               info->IsBlank() ? info->prev_real_map : info.get(); | 
| Josh Gao | 8a24311 | 2019-11-14 17:33:12 -0800 | [diff] [blame] | 164 |         } | 
| Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 165 |         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 Srbecky | f1932fd | 2019-09-16 22:36:55 +0100 | [diff] [blame] | 175 |       // that there will only every be a handful of these so waiting | 
| Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 176 |       // to destroy them is not too expensive. | 
| Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 177 |       saved_maps_.emplace_back(std::move(info)); | 
| David Srbecky | f1932fd | 2019-09-16 22:36:55 +0100 | [diff] [blame] | 178 |       search_map_idx = old_map_idx + 1; | 
| Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 179 |       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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 189 |     saved_maps_.emplace_back(std::move(maps_[i])); | 
| Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 190 |     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 Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 196 |   std::sort(maps_.begin(), maps_.end(), [](const auto& a, const auto& b) { | 
| Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 197 |     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 Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 209 | }  // namespace unwindstack |