| 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> | 
 | 22 | #include <sys/mman.h> | 
 | 23 | #include <sys/types.h> | 
 | 24 | #include <unistd.h> | 
 | 25 |  | 
 | 26 | #include <android-base/unique_fd.h> | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 27 | #include <procinfo/process_map.h> | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 28 |  | 
| Yabin Cui | d5b22c5 | 2018-02-22 17:11:31 -0800 | [diff] [blame] | 29 | #include <algorithm> | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 30 | #include <cctype> | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 31 | #include <memory> | 
 | 32 | #include <string> | 
 | 33 | #include <vector> | 
 | 34 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 35 | #include <unwindstack/Elf.h> | 
 | 36 | #include <unwindstack/Maps.h> | 
 | 37 | #include <unwindstack/Memory.h> | 
 | 38 |  | 
 | 39 | namespace unwindstack { | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 40 |  | 
 | 41 | MapInfo* Maps::Find(uint64_t pc) { | 
 | 42 |   if (maps_.empty()) { | 
 | 43 |     return nullptr; | 
 | 44 |   } | 
 | 45 |   size_t first = 0; | 
 | 46 |   size_t last = maps_.size(); | 
 | 47 |   while (first < last) { | 
 | 48 |     size_t index = (first + last) / 2; | 
| Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 49 |     MapInfo* cur = maps_[index]; | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 50 |     if (pc >= cur->start && pc < cur->end) { | 
 | 51 |       return cur; | 
 | 52 |     } else if (pc < cur->start) { | 
 | 53 |       last = index; | 
 | 54 |     } else { | 
 | 55 |       first = index + 1; | 
 | 56 |     } | 
 | 57 |   } | 
 | 58 |   return nullptr; | 
 | 59 | } | 
 | 60 |  | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 61 | bool Maps::Parse() { | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 62 |   return android::procinfo::ReadMapFile( | 
 | 63 |       GetMapsFile(), | 
 | 64 |       [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) { | 
 | 65 |         // Mark a device map in /dev/ and not in /dev/ashmem/ specially. | 
 | 66 |         if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) { | 
 | 67 |           flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP; | 
 | 68 |         } | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 69 |         maps_.push_back(new MapInfo(this, start, end, pgoff, flags, name)); | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 70 |       }); | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 71 | } | 
 | 72 |  | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 73 | void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, | 
 | 74 |                const std::string& name, uint64_t load_bias) { | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 75 |   MapInfo* map_info = new MapInfo(this, start, end, offset, flags, name); | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 76 |   map_info->load_bias = load_bias; | 
 | 77 |   maps_.push_back(map_info); | 
 | 78 | } | 
 | 79 |  | 
| Yabin Cui | d5b22c5 | 2018-02-22 17:11:31 -0800 | [diff] [blame] | 80 | void Maps::Sort() { | 
 | 81 |   std::sort(maps_.begin(), maps_.end(), | 
 | 82 |             [](const MapInfo* a, const MapInfo* b) { return a->start < b->start; }); | 
 | 83 | } | 
 | 84 |  | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 85 | Maps::~Maps() { | 
 | 86 |   for (auto& map : maps_) { | 
| Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 87 |     delete map; | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 88 |   } | 
 | 89 | } | 
 | 90 |  | 
 | 91 | bool BufferMaps::Parse() { | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 92 |   std::string content(buffer_); | 
 | 93 |   return android::procinfo::ReadMapFileContent( | 
 | 94 |       &content[0], | 
 | 95 |       [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) { | 
 | 96 |         // Mark a device map in /dev/ and not in /dev/ashmem/ specially. | 
 | 97 |         if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) { | 
 | 98 |           flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP; | 
 | 99 |         } | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 100 |         maps_.push_back(new MapInfo(this, start, end, pgoff, flags, name)); | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 101 |       }); | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 102 | } | 
 | 103 |  | 
 | 104 | const std::string RemoteMaps::GetMapsFile() const { | 
 | 105 |   return "/proc/" + std::to_string(pid_) + "/maps"; | 
 | 106 | } | 
 | 107 |  | 
| Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 108 | const std::string LocalUpdatableMaps::GetMapsFile() const { | 
 | 109 |   return "/proc/self/maps"; | 
 | 110 | } | 
 | 111 |  | 
 | 112 | bool LocalUpdatableMaps::Reparse() { | 
 | 113 |   // New maps will be added at the end without deleting the old ones. | 
 | 114 |   size_t last_map_idx = maps_.size(); | 
 | 115 |   if (!Parse()) { | 
 | 116 |     // Delete any maps added by the Parse call. | 
 | 117 |     for (size_t i = last_map_idx; i < maps_.size(); i++) { | 
 | 118 |       delete maps_[i]; | 
 | 119 |     } | 
 | 120 |     maps_.resize(last_map_idx); | 
 | 121 |     return false; | 
 | 122 |   } | 
 | 123 |  | 
 | 124 |   size_t total_entries = maps_.size(); | 
 | 125 |   size_t search_map_idx = 0; | 
 | 126 |   for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) { | 
 | 127 |     MapInfo* new_map_info = maps_[new_map_idx]; | 
 | 128 |     uint64_t start = new_map_info->start; | 
 | 129 |     uint64_t end = new_map_info->end; | 
 | 130 |     uint64_t flags = new_map_info->flags; | 
 | 131 |     std::string* name = &new_map_info->name; | 
 | 132 |     for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) { | 
 | 133 |       MapInfo* info = maps_[old_map_idx]; | 
 | 134 |       if (start == info->start && end == info->end && flags == info->flags && *name == info->name) { | 
 | 135 |         // No need to check | 
 | 136 |         search_map_idx = old_map_idx + 1; | 
 | 137 |         delete new_map_info; | 
 | 138 |         maps_[new_map_idx] = nullptr; | 
 | 139 |         total_entries--; | 
 | 140 |         break; | 
 | 141 |       } else if (info->start > start) { | 
 | 142 |         // Stop, there isn't going to be a match. | 
 | 143 |         search_map_idx = old_map_idx; | 
 | 144 |         break; | 
 | 145 |       } | 
 | 146 |  | 
 | 147 |       // Never delete these maps, they may be in use. The assumption is | 
 | 148 |       // that there will only every be a handfull of these so waiting | 
 | 149 |       // to destroy them is not too expensive. | 
 | 150 |       saved_maps_.push_back(info); | 
 | 151 |       maps_[old_map_idx] = nullptr; | 
 | 152 |       total_entries--; | 
 | 153 |     } | 
 | 154 |     if (search_map_idx >= last_map_idx) { | 
 | 155 |       break; | 
 | 156 |     } | 
 | 157 |   } | 
 | 158 |  | 
 | 159 |   // Now move out any of the maps that never were found. | 
 | 160 |   for (size_t i = search_map_idx; i < last_map_idx; i++) { | 
 | 161 |     saved_maps_.push_back(maps_[i]); | 
 | 162 |     maps_[i] = nullptr; | 
 | 163 |     total_entries--; | 
 | 164 |   } | 
 | 165 |  | 
 | 166 |   // Sort all of the values such that the nullptrs wind up at the end, then | 
 | 167 |   // resize them away. | 
 | 168 |   std::sort(maps_.begin(), maps_.end(), [](const auto* a, const auto* b) { | 
 | 169 |     if (a == nullptr) { | 
 | 170 |       return false; | 
 | 171 |     } else if (b == nullptr) { | 
 | 172 |       return true; | 
 | 173 |     } | 
 | 174 |     return a->start < b->start; | 
 | 175 |   }); | 
 | 176 |   maps_.resize(total_entries); | 
 | 177 |  | 
 | 178 |   return true; | 
 | 179 | } | 
 | 180 |  | 
 | 181 | LocalUpdatableMaps::~LocalUpdatableMaps() { | 
 | 182 |   for (auto map_info : saved_maps_) { | 
 | 183 |     delete map_info; | 
 | 184 |   } | 
 | 185 | } | 
 | 186 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 187 | }  // namespace unwindstack |