| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2017 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 |  | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 17 | #include <stdint.h> | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 18 | #include <sys/mman.h> | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 19 | #include <sys/types.h> | 
 | 20 | #include <unistd.h> | 
 | 21 |  | 
 | 22 | #include <memory> | 
| Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 23 | #include <mutex> | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 24 | #include <string> | 
 | 25 |  | 
| Christopher Ferris | b1c9c20 | 2019-01-25 14:28:13 -0800 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> | 
 | 27 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 28 | #include <unwindstack/Elf.h> | 
 | 29 | #include <unwindstack/MapInfo.h> | 
 | 30 | #include <unwindstack/Maps.h> | 
| Casey Dahlin | 6b95a0e | 2019-03-12 17:50:52 -0700 | [diff] [blame] | 31 |  | 
 | 32 | #include "MemoryFileAtOffset.h" | 
 | 33 | #include "MemoryRange.h" | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 34 |  | 
 | 35 | namespace unwindstack { | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 36 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 37 | bool MapInfo::InitFileMemoryFromPreviousReadOnlyMap(MemoryFileAtOffset* memory) { | 
 | 38 |   // One last attempt, see if the previous map is read-only with the | 
 | 39 |   // same name and stretches across this map. | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 40 |   if (prev_map == nullptr || prev_map->flags != PROT_READ) { | 
 | 41 |     return false; | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 42 |   } | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 43 |  | 
 | 44 |   uint64_t map_size = end - prev_map->end; | 
 | 45 |   if (!memory->Init(name, prev_map->offset, map_size)) { | 
 | 46 |     return false; | 
 | 47 |   } | 
 | 48 |  | 
 | 49 |   uint64_t max_size; | 
 | 50 |   if (!Elf::GetInfo(memory, &max_size) || max_size < map_size) { | 
 | 51 |     return false; | 
 | 52 |   } | 
 | 53 |  | 
 | 54 |   if (!memory->Init(name, prev_map->offset, max_size)) { | 
 | 55 |     return false; | 
 | 56 |   } | 
 | 57 |  | 
 | 58 |   elf_offset = offset - prev_map->offset; | 
 | 59 |   elf_start_offset = prev_map->offset; | 
 | 60 |   return true; | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 61 | } | 
 | 62 |  | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 63 | Memory* MapInfo::GetFileMemory() { | 
 | 64 |   std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset); | 
 | 65 |   if (offset == 0) { | 
 | 66 |     if (memory->Init(name, 0)) { | 
 | 67 |       return memory.release(); | 
 | 68 |     } | 
 | 69 |     return nullptr; | 
 | 70 |   } | 
 | 71 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 72 |   // These are the possibilities when the offset is non-zero. | 
 | 73 |   // - There is an elf file embedded in a file, and the offset is the | 
 | 74 |   //   the start of the elf in the file. | 
 | 75 |   // - There is an elf file embedded in a file, and the offset is the | 
 | 76 |   //   the start of the executable part of the file. The actual start | 
 | 77 |   //   of the elf is in the read-only segment preceeding this map. | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 78 |   // - The whole file is an elf file, and the offset needs to be saved. | 
 | 79 |   // | 
 | 80 |   // Map in just the part of the file for the map. If this is not | 
 | 81 |   // a valid elf, then reinit as if the whole file is an elf file. | 
 | 82 |   // If the offset is a valid elf, then determine the size of the map | 
 | 83 |   // and reinit to that size. This is needed because the dynamic linker | 
 | 84 |   // only maps in a portion of the original elf, and never the symbol | 
 | 85 |   // file data. | 
 | 86 |   uint64_t map_size = end - start; | 
 | 87 |   if (!memory->Init(name, offset, map_size)) { | 
 | 88 |     return nullptr; | 
 | 89 |   } | 
 | 90 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 91 |   // Check if the start of this map is an embedded elf. | 
 | 92 |   uint64_t max_size = 0; | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 93 |   if (Elf::GetInfo(memory.get(), &max_size)) { | 
| Christopher Ferris | 86f2d9d | 2019-03-12 15:17:36 -0700 | [diff] [blame] | 94 |     elf_start_offset = offset; | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 95 |     if (max_size > map_size) { | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 96 |       if (memory->Init(name, offset, max_size)) { | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 97 |         return memory.release(); | 
 | 98 |       } | 
 | 99 |       // Try to reinit using the default map_size. | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 100 |       if (memory->Init(name, offset, map_size)) { | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 101 |         return memory.release(); | 
 | 102 |       } | 
| Christopher Ferris | 86f2d9d | 2019-03-12 15:17:36 -0700 | [diff] [blame] | 103 |       elf_start_offset = 0; | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 104 |       return nullptr; | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 105 |     } | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 106 |     return memory.release(); | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 107 |   } | 
 | 108 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 109 |   // No elf at offset, try to init as if the whole file is an elf. | 
 | 110 |   if (memory->Init(name, 0) && Elf::IsValidElf(memory.get())) { | 
 | 111 |     elf_offset = offset; | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 112 |     // Need to check how to set the elf start offset. If this map is not | 
 | 113 |     // the r-x map of a r-- map, then use the real offset value. Otherwise, | 
 | 114 |     // use 0. | 
 | 115 |     if (prev_map == nullptr || prev_map->offset != 0 || prev_map->flags != PROT_READ || | 
 | 116 |         prev_map->name != name) { | 
 | 117 |       elf_start_offset = offset; | 
 | 118 |     } | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 119 |     return memory.release(); | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 120 |   } | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 121 |  | 
 | 122 |   // See if the map previous to this one contains a read-only map | 
 | 123 |   // that represents the real start of the elf data. | 
 | 124 |   if (InitFileMemoryFromPreviousReadOnlyMap(memory.get())) { | 
 | 125 |     return memory.release(); | 
 | 126 |   } | 
 | 127 |  | 
 | 128 |   // Failed to find elf at start of file or at read-only map, return | 
 | 129 |   // file object from the current map. | 
 | 130 |   if (memory->Init(name, offset, map_size)) { | 
 | 131 |     return memory.release(); | 
 | 132 |   } | 
 | 133 |   return nullptr; | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 134 | } | 
 | 135 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 136 | Memory* MapInfo::CreateMemory(const std::shared_ptr<Memory>& process_memory) { | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 137 |   if (end <= start) { | 
 | 138 |     return nullptr; | 
 | 139 |   } | 
 | 140 |  | 
 | 141 |   elf_offset = 0; | 
 | 142 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 143 |   // Fail on device maps. | 
 | 144 |   if (flags & MAPS_FLAGS_DEVICE_MAP) { | 
 | 145 |     return nullptr; | 
 | 146 |   } | 
 | 147 |  | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 148 |   // First try and use the file associated with the info. | 
 | 149 |   if (!name.empty()) { | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 150 |     Memory* memory = GetFileMemory(); | 
 | 151 |     if (memory != nullptr) { | 
 | 152 |       return memory; | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 153 |     } | 
 | 154 |   } | 
 | 155 |  | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 156 |   if (process_memory == nullptr) { | 
 | 157 |     return nullptr; | 
 | 158 |   } | 
 | 159 |  | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 160 |   // Need to verify that this elf is valid. It's possible that | 
 | 161 |   // only part of the elf file to be mapped into memory is in the executable | 
 | 162 |   // map. In this case, there will be another read-only map that includes the | 
 | 163 |   // first part of the elf file. This is done if the linker rosegment | 
 | 164 |   // option is used. | 
 | 165 |   std::unique_ptr<MemoryRange> memory(new MemoryRange(process_memory, start, end - start, 0)); | 
| Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 166 |   if (Elf::IsValidElf(memory.get())) { | 
| Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame] | 167 |     memory_backed_elf = true; | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 168 |     return memory.release(); | 
 | 169 |   } | 
 | 170 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 171 |   // Find the read-only map by looking at the previous map. The linker | 
 | 172 |   // doesn't guarantee that this invariant will always be true. However, | 
 | 173 |   // if that changes, there is likely something else that will change and | 
 | 174 |   // break something. | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 175 |   if (offset == 0 || name.empty() || prev_map == nullptr || prev_map->name != name || | 
 | 176 |       prev_map->offset >= offset) { | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 177 |     return nullptr; | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 178 |   } | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 179 |  | 
 | 180 |   // Make sure that relative pc values are corrected properly. | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 181 |   elf_offset = offset - prev_map->offset; | 
 | 182 |   // Use this as the elf start offset, otherwise, you always get offsets into | 
 | 183 |   // the r-x section, which is not quite the right information. | 
 | 184 |   elf_start_offset = prev_map->offset; | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 185 |  | 
 | 186 |   MemoryRanges* ranges = new MemoryRanges; | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 187 |   ranges->Insert( | 
 | 188 |       new MemoryRange(process_memory, prev_map->start, prev_map->end - prev_map->start, 0)); | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 189 |   ranges->Insert(new MemoryRange(process_memory, start, end - start, elf_offset)); | 
 | 190 |  | 
| Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame] | 191 |   memory_backed_elf = true; | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 192 |   return ranges; | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 193 | } | 
 | 194 |  | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 195 | Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, ArchEnum expected_arch) { | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 196 |   { | 
 | 197 |     // Make sure no other thread is trying to add the elf to this map. | 
 | 198 |     std::lock_guard<std::mutex> guard(mutex_); | 
| Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 199 |  | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 200 |     if (elf.get() != nullptr) { | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 201 |       return elf.get(); | 
 | 202 |     } | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 203 |  | 
 | 204 |     bool locked = false; | 
 | 205 |     if (Elf::CachingEnabled() && !name.empty()) { | 
 | 206 |       Elf::CacheLock(); | 
 | 207 |       locked = true; | 
 | 208 |       if (Elf::CacheGet(this)) { | 
 | 209 |         Elf::CacheUnlock(); | 
 | 210 |         return elf.get(); | 
 | 211 |       } | 
 | 212 |     } | 
 | 213 |  | 
 | 214 |     Memory* memory = CreateMemory(process_memory); | 
 | 215 |     if (locked) { | 
 | 216 |       if (Elf::CacheAfterCreateMemory(this)) { | 
 | 217 |         delete memory; | 
 | 218 |         Elf::CacheUnlock(); | 
 | 219 |         return elf.get(); | 
 | 220 |       } | 
 | 221 |     } | 
 | 222 |     elf.reset(new Elf(memory)); | 
 | 223 |     // If the init fails, keep the elf around as an invalid object so we | 
 | 224 |     // don't try to reinit the object. | 
 | 225 |     elf->Init(); | 
 | 226 |     if (elf->valid() && expected_arch != elf->arch()) { | 
 | 227 |       // Make the elf invalid, mismatch between arch and expected arch. | 
 | 228 |       elf->Invalidate(); | 
 | 229 |     } | 
 | 230 |  | 
 | 231 |     if (locked) { | 
 | 232 |       Elf::CacheAdd(this); | 
 | 233 |       Elf::CacheUnlock(); | 
 | 234 |     } | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 235 |   } | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 236 |  | 
| Christopher Ferris | d49499d | 2019-06-10 18:37:32 -0700 | [diff] [blame] | 237 |   if (!elf->valid()) { | 
 | 238 |     elf_start_offset = offset; | 
 | 239 |   } else if (prev_map != nullptr && elf_start_offset != offset && | 
 | 240 |              prev_map->offset == elf_start_offset && prev_map->name == name) { | 
 | 241 |     // If there is a read-only map then a read-execute map that represents the | 
 | 242 |     // same elf object, make sure the previous map is using the same elf | 
 | 243 |     // object if it hasn't already been set. | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 244 |     std::lock_guard<std::mutex> guard(prev_map->mutex_); | 
 | 245 |     if (prev_map->elf.get() == nullptr) { | 
 | 246 |       prev_map->elf = elf; | 
| Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame] | 247 |       prev_map->memory_backed_elf = memory_backed_elf; | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 248 |     } | 
 | 249 |   } | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 250 |   return elf.get(); | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 251 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 252 |  | 
| Christopher Ferris | eb0772f | 2018-12-05 15:57:02 -0800 | [diff] [blame] | 253 | bool MapInfo::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) { | 
 | 254 |   { | 
 | 255 |     // Make sure no other thread is trying to update this elf object. | 
 | 256 |     std::lock_guard<std::mutex> guard(mutex_); | 
 | 257 |     if (elf == nullptr) { | 
 | 258 |       return false; | 
 | 259 |     } | 
 | 260 |   } | 
 | 261 |   // No longer need the lock, once the elf object is created, it is not deleted | 
 | 262 |   // until this object is deleted. | 
 | 263 |   return elf->GetFunctionName(addr, name, func_offset); | 
 | 264 | } | 
 | 265 |  | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 266 | uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) { | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 267 |   int64_t cur_load_bias = load_bias.load(); | 
 | 268 |   if (cur_load_bias != INT64_MAX) { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 269 |     return cur_load_bias; | 
 | 270 |   } | 
 | 271 |  | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 272 |   { | 
 | 273 |     // Make sure no other thread is trying to add the elf to this map. | 
 | 274 |     std::lock_guard<std::mutex> guard(mutex_); | 
 | 275 |     if (elf != nullptr) { | 
 | 276 |       if (elf->valid()) { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 277 |         cur_load_bias = elf->GetLoadBias(); | 
 | 278 |         load_bias = cur_load_bias; | 
 | 279 |         return cur_load_bias; | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 280 |       } else { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 281 |         load_bias = 0; | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 282 |         return 0; | 
 | 283 |       } | 
 | 284 |     } | 
 | 285 |   } | 
 | 286 |  | 
 | 287 |   // Call lightweight static function that will only read enough of the | 
 | 288 |   // elf data to get the load bias. | 
 | 289 |   std::unique_ptr<Memory> memory(CreateMemory(process_memory)); | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 290 |   cur_load_bias = Elf::GetLoadBias(memory.get()); | 
 | 291 |   load_bias = cur_load_bias; | 
 | 292 |   return cur_load_bias; | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 293 | } | 
 | 294 |  | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 295 | MapInfo::~MapInfo() { | 
 | 296 |   uintptr_t id = build_id.load(); | 
 | 297 |   if (id != 0) { | 
 | 298 |     delete reinterpret_cast<std::string*>(id); | 
 | 299 |   } | 
 | 300 | } | 
 | 301 |  | 
 | 302 | std::string MapInfo::GetBuildID() { | 
 | 303 |   uintptr_t id = build_id.load(); | 
| Christopher Ferris | d1d973b | 2019-06-19 18:41:37 -0700 | [diff] [blame] | 304 |   if (id != 0) { | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 305 |     return *reinterpret_cast<std::string*>(id); | 
 | 306 |   } | 
 | 307 |  | 
 | 308 |   // No need to lock, at worst if multiple threads do this at the same | 
 | 309 |   // time it should be detected and only one thread should win and | 
 | 310 |   // save the data. | 
 | 311 |   std::unique_ptr<std::string> cur_build_id(new std::string); | 
 | 312 |  | 
 | 313 |   // Now need to see if the elf object exists. | 
 | 314 |   // Make sure no other thread is trying to add the elf to this map. | 
 | 315 |   mutex_.lock(); | 
 | 316 |   Elf* elf_obj = elf.get(); | 
 | 317 |   mutex_.unlock(); | 
 | 318 |   if (elf_obj != nullptr) { | 
 | 319 |     *cur_build_id = elf_obj->GetBuildID(); | 
 | 320 |   } else { | 
 | 321 |     // This will only work if we can get the file associated with this memory. | 
 | 322 |     // If this is only available in memory, then the section name information | 
 | 323 |     // is not present and we will not be able to find the build id info. | 
 | 324 |     std::unique_ptr<Memory> memory(GetFileMemory()); | 
 | 325 |     if (memory != nullptr) { | 
 | 326 |       *cur_build_id = Elf::GetBuildID(memory.get()); | 
 | 327 |     } | 
 | 328 |   } | 
 | 329 |  | 
 | 330 |   id = reinterpret_cast<uintptr_t>(cur_build_id.get()); | 
 | 331 |   uintptr_t expected_id = 0; | 
 | 332 |   if (build_id.compare_exchange_weak(expected_id, id)) { | 
 | 333 |     // Value saved, so make sure the memory is not freed. | 
 | 334 |     cur_build_id.release(); | 
 | 335 |   } | 
 | 336 |   return *reinterpret_cast<std::string*>(id); | 
 | 337 | } | 
 | 338 |  | 
| Christopher Ferris | b1c9c20 | 2019-01-25 14:28:13 -0800 | [diff] [blame] | 339 | std::string MapInfo::GetPrintableBuildID() { | 
 | 340 |   std::string raw_build_id = GetBuildID(); | 
 | 341 |   if (raw_build_id.empty()) { | 
 | 342 |     return ""; | 
 | 343 |   } | 
 | 344 |   std::string printable_build_id; | 
 | 345 |   for (const char& c : raw_build_id) { | 
| Christopher Ferris | ce34d62 | 2019-01-30 10:55:27 -0800 | [diff] [blame] | 346 |     // Use %hhx to avoid sign extension on abis that have signed chars. | 
 | 347 |     printable_build_id += android::base::StringPrintf("%02hhx", c); | 
| Christopher Ferris | b1c9c20 | 2019-01-25 14:28:13 -0800 | [diff] [blame] | 348 |   } | 
 | 349 |   return printable_build_id; | 
 | 350 | } | 
 | 351 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 352 | }  // namespace unwindstack |