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