Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "BacktraceOffline.h" |
| 18 | |
| 19 | extern "C" { |
| 20 | #define UNW_REMOTE_ONLY |
| 21 | #include <dwarf.h> |
| 22 | } |
| 23 | |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame^] | 24 | #include <pthread.h> |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 25 | #include <stdint.h> |
Yabin Cui | 2ad59db | 2015-12-08 18:43:00 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 27 | #include <string.h> |
Yabin Cui | 2ad59db | 2015-12-08 18:43:00 -0800 | [diff] [blame] | 28 | #include <sys/stat.h> |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <ucontext.h> |
| 31 | #include <unistd.h> |
| 32 | |
Yabin Cui | b791a76 | 2016-03-18 18:46:08 -0700 | [diff] [blame] | 33 | #include <memory> |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 34 | #include <string> |
| 35 | #include <vector> |
| 36 | |
Yabin Cui | b791a76 | 2016-03-18 18:46:08 -0700 | [diff] [blame] | 37 | #include <android-base/file.h> |
Colin Cross | 06d3149 | 2016-12-15 12:55:03 -0800 | [diff] [blame] | 38 | #include <android-base/macros.h> |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 39 | #include <backtrace/Backtrace.h> |
| 40 | #include <backtrace/BacktraceMap.h> |
Yabin Cui | b791a76 | 2016-03-18 18:46:08 -0700 | [diff] [blame] | 41 | #include <ziparchive/zip_archive.h> |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 42 | |
| 43 | #pragma clang diagnostic push |
| 44 | #pragma clang diagnostic ignored "-Wunused-parameter" |
| 45 | |
| 46 | #include <llvm/ADT/StringRef.h> |
| 47 | #include <llvm/Object/Binary.h> |
| 48 | #include <llvm/Object/ELFObjectFile.h> |
| 49 | #include <llvm/Object/ObjectFile.h> |
| 50 | |
| 51 | #pragma clang diagnostic pop |
| 52 | |
| 53 | #include "BacktraceLog.h" |
| 54 | |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 55 | struct EhFrame { |
| 56 | uint64_t hdr_vaddr; |
| 57 | uint64_t vaddr; |
| 58 | uint64_t fde_table_offset; |
| 59 | uintptr_t min_func_vaddr; |
| 60 | std::vector<uint8_t> hdr_data; |
| 61 | std::vector<uint8_t> data; |
| 62 | }; |
| 63 | |
| 64 | struct ArmIdxEntry { |
| 65 | uint32_t func_offset; |
| 66 | uint32_t value; |
| 67 | }; |
| 68 | |
| 69 | struct ArmExidx { |
| 70 | uint64_t exidx_vaddr; |
| 71 | uint64_t extab_vaddr; |
| 72 | std::vector<ArmIdxEntry> exidx_data; |
| 73 | std::vector<uint8_t> extab_data; |
| 74 | // There is a one-to-one map from exidx_data.func_offset to func_vaddr_array. |
| 75 | std::vector<uint32_t> func_vaddr_array; |
| 76 | }; |
| 77 | |
| 78 | struct DebugFrameInfo { |
| 79 | bool has_arm_exidx; |
| 80 | bool has_eh_frame; |
| 81 | bool has_debug_frame; |
| 82 | bool has_gnu_debugdata; |
| 83 | |
| 84 | EhFrame eh_frame; |
| 85 | ArmExidx arm_exidx; |
| 86 | |
| 87 | uint64_t min_vaddr; |
| 88 | uint64_t text_end_vaddr; |
| 89 | |
| 90 | DebugFrameInfo() : has_arm_exidx(false), has_eh_frame(false), |
| 91 | has_debug_frame(false), has_gnu_debugdata(false) { } |
| 92 | }; |
| 93 | |
| 94 | static std::unordered_map<std::string, std::unique_ptr<DebugFrameInfo>>& g_debug_frames = |
| 95 | *new std::unordered_map<std::string, std::unique_ptr<DebugFrameInfo>>; |
| 96 | |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 97 | void Space::Clear() { |
| 98 | start = 0; |
| 99 | end = 0; |
| 100 | data = nullptr; |
| 101 | } |
| 102 | |
| 103 | size_t Space::Read(uint64_t addr, uint8_t* buffer, size_t size) { |
| 104 | if (addr >= start && addr < end) { |
| 105 | size_t read_size = std::min(size, static_cast<size_t>(end - addr)); |
| 106 | memcpy(buffer, data + (addr - start), read_size); |
| 107 | return read_size; |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int FindProcInfo(unw_addr_space_t addr_space, unw_word_t ip, unw_proc_info* proc_info, |
| 113 | int need_unwind_info, void* arg) { |
| 114 | BacktraceOffline* backtrace = reinterpret_cast<BacktraceOffline*>(arg); |
| 115 | bool result = backtrace->FindProcInfo(addr_space, ip, proc_info, need_unwind_info); |
| 116 | return result ? 0 : -UNW_EINVAL; |
| 117 | } |
| 118 | |
| 119 | static void PutUnwindInfo(unw_addr_space_t, unw_proc_info_t*, void*) { |
| 120 | } |
| 121 | |
| 122 | static int GetDynInfoListAddr(unw_addr_space_t, unw_word_t*, void*) { |
| 123 | return -UNW_ENOINFO; |
| 124 | } |
| 125 | |
| 126 | static int AccessMem(unw_addr_space_t, unw_word_t addr, unw_word_t* value, int write, void* arg) { |
| 127 | if (write == 1) { |
| 128 | return -UNW_EINVAL; |
| 129 | } |
| 130 | BacktraceOffline* backtrace = reinterpret_cast<BacktraceOffline*>(arg); |
| 131 | *value = 0; |
| 132 | size_t read_size = backtrace->Read(addr, reinterpret_cast<uint8_t*>(value), sizeof(unw_word_t)); |
| 133 | // Strictly we should check if read_size matches sizeof(unw_word_t), but it is possible in |
| 134 | // .eh_frame_hdr that the section can end at a position not aligned in sizeof(unw_word_t), and |
| 135 | // we should permit the read at the end of the section. |
| 136 | return (read_size > 0u ? 0 : -UNW_EINVAL); |
| 137 | } |
| 138 | |
| 139 | static int AccessReg(unw_addr_space_t, unw_regnum_t unwind_reg, unw_word_t* value, int write, |
| 140 | void* arg) { |
| 141 | if (write == 1) { |
| 142 | return -UNW_EINVAL; |
| 143 | } |
| 144 | BacktraceOffline* backtrace = reinterpret_cast<BacktraceOffline*>(arg); |
| 145 | uint64_t reg_value; |
| 146 | bool result = backtrace->ReadReg(unwind_reg, ®_value); |
| 147 | if (result) { |
| 148 | *value = static_cast<unw_word_t>(reg_value); |
| 149 | } |
| 150 | return result ? 0 : -UNW_EINVAL; |
| 151 | } |
| 152 | |
| 153 | static int AccessFpReg(unw_addr_space_t, unw_regnum_t, unw_fpreg_t*, int, void*) { |
| 154 | return -UNW_EINVAL; |
| 155 | } |
| 156 | |
| 157 | static int Resume(unw_addr_space_t, unw_cursor_t*, void*) { |
| 158 | return -UNW_EINVAL; |
| 159 | } |
| 160 | |
| 161 | static int GetProcName(unw_addr_space_t, unw_word_t, char*, size_t, unw_word_t*, void*) { |
| 162 | return -UNW_EINVAL; |
| 163 | } |
| 164 | |
| 165 | static unw_accessors_t accessors = { |
| 166 | .find_proc_info = FindProcInfo, |
| 167 | .put_unwind_info = PutUnwindInfo, |
| 168 | .get_dyn_info_list_addr = GetDynInfoListAddr, |
| 169 | .access_mem = AccessMem, |
| 170 | .access_reg = AccessReg, |
| 171 | .access_fpreg = AccessFpReg, |
| 172 | .resume = Resume, |
| 173 | .get_proc_name = GetProcName, |
| 174 | }; |
| 175 | |
| 176 | bool BacktraceOffline::Unwind(size_t num_ignore_frames, ucontext_t* context) { |
| 177 | if (context == nullptr) { |
| 178 | BACK_LOGW("The context is needed for offline backtracing."); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 179 | error_ = BACKTRACE_UNWIND_ERROR_NO_CONTEXT; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | context_ = context; |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 183 | error_ = BACKTRACE_UNWIND_NO_ERROR; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 184 | |
| 185 | unw_addr_space_t addr_space = unw_create_addr_space(&accessors, 0); |
| 186 | unw_cursor_t cursor; |
| 187 | int ret = unw_init_remote(&cursor, addr_space, this); |
| 188 | if (ret != 0) { |
| 189 | BACK_LOGW("unw_init_remote failed %d", ret); |
| 190 | unw_destroy_addr_space(addr_space); |
Christopher Ferris | 206a3b9 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 191 | error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | size_t num_frames = 0; |
| 195 | do { |
| 196 | unw_word_t pc; |
| 197 | ret = unw_get_reg(&cursor, UNW_REG_IP, &pc); |
| 198 | if (ret < 0) { |
| 199 | BACK_LOGW("Failed to read IP %d", ret); |
| 200 | break; |
| 201 | } |
| 202 | unw_word_t sp; |
| 203 | ret = unw_get_reg(&cursor, UNW_REG_SP, &sp); |
| 204 | if (ret < 0) { |
| 205 | BACK_LOGW("Failed to read SP %d", ret); |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | if (num_ignore_frames == 0) { |
| 210 | frames_.resize(num_frames + 1); |
| 211 | backtrace_frame_data_t* frame = &frames_[num_frames]; |
| 212 | frame->num = num_frames; |
| 213 | frame->pc = static_cast<uintptr_t>(pc); |
| 214 | frame->sp = static_cast<uintptr_t>(sp); |
| 215 | frame->stack_size = 0; |
| 216 | |
| 217 | if (num_frames > 0) { |
| 218 | backtrace_frame_data_t* prev = &frames_[num_frames - 1]; |
| 219 | prev->stack_size = frame->sp - prev->sp; |
| 220 | } |
| 221 | frame->func_name = GetFunctionName(frame->pc, &frame->func_offset); |
| 222 | FillInMap(frame->pc, &frame->map); |
| 223 | num_frames++; |
| 224 | } else { |
| 225 | num_ignore_frames--; |
| 226 | } |
| 227 | ret = unw_step(&cursor); |
| 228 | } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES); |
| 229 | |
| 230 | unw_destroy_addr_space(addr_space); |
| 231 | context_ = nullptr; |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | bool BacktraceOffline::ReadWord(uintptr_t ptr, word_t* out_value) { |
| 236 | size_t bytes_read = Read(ptr, reinterpret_cast<uint8_t*>(out_value), sizeof(word_t)); |
| 237 | return bytes_read == sizeof(word_t); |
| 238 | } |
| 239 | |
| 240 | size_t BacktraceOffline::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) { |
| 241 | // Normally, libunwind needs stack information and call frame information to do remote unwinding. |
| 242 | // If call frame information is stored in .debug_frame, libunwind can read it from file |
| 243 | // by itself. If call frame information is stored in .eh_frame, we need to provide data in |
| 244 | // .eh_frame/.eh_frame_hdr sections. |
| 245 | // The order of readings below doesn't matter, as the spaces don't overlap with each other. |
| 246 | size_t read_size = eh_frame_hdr_space_.Read(addr, buffer, bytes); |
| 247 | if (read_size != 0) { |
| 248 | return read_size; |
| 249 | } |
| 250 | read_size = eh_frame_space_.Read(addr, buffer, bytes); |
| 251 | if (read_size != 0) { |
| 252 | return read_size; |
| 253 | } |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 254 | read_size = arm_exidx_space_.Read(addr, buffer, bytes); |
| 255 | if (read_size != 0) { |
| 256 | return read_size; |
| 257 | } |
| 258 | read_size = arm_extab_space_.Read(addr, buffer, bytes); |
| 259 | if (read_size != 0) { |
| 260 | return read_size; |
| 261 | } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 262 | read_size = stack_space_.Read(addr, buffer, bytes); |
| 263 | return read_size; |
| 264 | } |
| 265 | |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 266 | bool BacktraceOffline::FindProcInfo(unw_addr_space_t addr_space, uint64_t ip, |
| 267 | unw_proc_info_t* proc_info, int need_unwind_info) { |
| 268 | backtrace_map_t map; |
| 269 | FillInMap(ip, &map); |
| 270 | if (!BacktraceMap::IsValid(map)) { |
| 271 | return false; |
| 272 | } |
| 273 | const std::string& filename = map.name; |
| 274 | DebugFrameInfo* debug_frame = GetDebugFrameInFile(filename); |
| 275 | if (debug_frame == nullptr) { |
| 276 | return false; |
| 277 | } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 278 | |
| 279 | eh_frame_hdr_space_.Clear(); |
| 280 | eh_frame_space_.Clear(); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 281 | arm_exidx_space_.Clear(); |
| 282 | arm_extab_space_.Clear(); |
| 283 | |
| 284 | // vaddr in the elf file. |
| 285 | uint64_t ip_vaddr = ip - map.start + debug_frame->min_vaddr; |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 286 | |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame^] | 287 | // The unwind info can come from .ARM.exidx or .eh_frame, or .debug_frame/.gnu_debugdata. |
| 288 | // First check .eh_frame/.debug_frame, then check .ARM.exidx. Because .eh_frame/.debug_frame has |
| 289 | // function range for each entry, by matching ip address with the function range, we know exactly |
| 290 | // whether the ip address hits an entry. But .ARM.exidx doesn't have function range for each |
| 291 | // entry, it thinks that an ip address hits an entry when (entry.addr <= ip < next_entry.addr). |
| 292 | // To prevent ip addresses hit in .eh_frame/.debug_frame being regarded as addresses hit in |
| 293 | // .ARM.exidx, we need to check .eh_frame/.debug_frame first. |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 294 | if (debug_frame->has_eh_frame) { |
| 295 | if (ip_vaddr >= debug_frame->eh_frame.min_func_vaddr && |
| 296 | ip_vaddr < debug_frame->text_end_vaddr) { |
| 297 | // Prepare eh_frame_hdr space and eh_frame space. |
| 298 | eh_frame_hdr_space_.start = ip - ip_vaddr + debug_frame->eh_frame.hdr_vaddr; |
| 299 | eh_frame_hdr_space_.end = |
| 300 | eh_frame_hdr_space_.start + debug_frame->eh_frame.hdr_data.size(); |
| 301 | eh_frame_hdr_space_.data = debug_frame->eh_frame.hdr_data.data(); |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 302 | eh_frame_space_.start = ip - ip_vaddr + debug_frame->eh_frame.vaddr; |
| 303 | eh_frame_space_.end = eh_frame_space_.start + debug_frame->eh_frame.data.size(); |
| 304 | eh_frame_space_.data = debug_frame->eh_frame.data.data(); |
| 305 | |
| 306 | unw_dyn_info di; |
| 307 | memset(&di, '\0', sizeof(di)); |
| 308 | di.start_ip = map.start; |
| 309 | di.end_ip = map.end; |
| 310 | di.format = UNW_INFO_FORMAT_REMOTE_TABLE; |
| 311 | di.u.rti.name_ptr = 0; |
| 312 | di.u.rti.segbase = eh_frame_hdr_space_.start; |
| 313 | di.u.rti.table_data = |
| 314 | eh_frame_hdr_space_.start + debug_frame->eh_frame.fde_table_offset; |
| 315 | di.u.rti.table_len = (eh_frame_hdr_space_.end - di.u.rti.table_data) / sizeof(unw_word_t); |
| 316 | // TODO: Do it ourselves is more efficient than calling this function. |
| 317 | int ret = dwarf_search_unwind_table(addr_space, ip, &di, proc_info, need_unwind_info, this); |
| 318 | if (ret == 0) { |
| 319 | return true; |
| 320 | } |
| 321 | } |
| 322 | } |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 323 | if (debug_frame->has_debug_frame || debug_frame->has_gnu_debugdata) { |
| 324 | unw_dyn_info_t di; |
| 325 | unw_word_t segbase = map.start - map.offset; |
| 326 | // TODO: http://b/32916571 |
| 327 | // TODO: Do it ourselves is more efficient than calling libunwind functions. |
| 328 | int found = dwarf_find_debug_frame(0, &di, ip, segbase, filename.c_str(), map.start, map.end); |
| 329 | if (found == 1) { |
| 330 | int ret = dwarf_search_unwind_table(addr_space, ip, &di, proc_info, need_unwind_info, this); |
| 331 | if (ret == 0) { |
| 332 | return true; |
| 333 | } |
| 334 | } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 335 | } |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame^] | 336 | |
| 337 | if (debug_frame->has_arm_exidx) { |
| 338 | auto& func_vaddrs = debug_frame->arm_exidx.func_vaddr_array; |
| 339 | if (ip_vaddr >= func_vaddrs[0] && ip_vaddr < debug_frame->text_end_vaddr) { |
| 340 | // Use binary search to find the correct function. |
| 341 | auto it = std::upper_bound(func_vaddrs.begin(), func_vaddrs.end(), |
| 342 | static_cast<uint32_t>(ip_vaddr)); |
| 343 | if (it != func_vaddrs.begin()) { |
| 344 | --it; |
| 345 | // Found the exidx entry. |
| 346 | size_t index = it - func_vaddrs.begin(); |
| 347 | proc_info->start_ip = *it; |
| 348 | proc_info->format = UNW_INFO_FORMAT_ARM_EXIDX; |
| 349 | proc_info->unwind_info = reinterpret_cast<void*>( |
| 350 | static_cast<uintptr_t>(index * sizeof(ArmIdxEntry) + |
| 351 | debug_frame->arm_exidx.exidx_vaddr + |
| 352 | debug_frame->min_vaddr)); |
| 353 | eh_frame_hdr_space_.Clear(); |
| 354 | eh_frame_space_.Clear(); |
| 355 | // Prepare arm_exidx space and arm_extab space. |
| 356 | arm_exidx_space_.start = debug_frame->min_vaddr + debug_frame->arm_exidx.exidx_vaddr; |
| 357 | arm_exidx_space_.end = arm_exidx_space_.start + |
| 358 | debug_frame->arm_exidx.exidx_data.size() * sizeof(ArmIdxEntry); |
| 359 | arm_exidx_space_.data = reinterpret_cast<const uint8_t*>( |
| 360 | debug_frame->arm_exidx.exidx_data.data()); |
| 361 | |
| 362 | arm_extab_space_.start = debug_frame->min_vaddr + debug_frame->arm_exidx.extab_vaddr; |
| 363 | arm_extab_space_.end = arm_extab_space_.start + |
| 364 | debug_frame->arm_exidx.extab_data.size(); |
| 365 | arm_extab_space_.data = debug_frame->arm_exidx.extab_data.data(); |
| 366 | return true; |
| 367 | } |
| 368 | } |
| 369 | } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | |
| 373 | bool BacktraceOffline::ReadReg(size_t reg, uint64_t* value) { |
| 374 | bool result = true; |
| 375 | #if defined(__arm__) |
| 376 | switch (reg) { |
| 377 | case UNW_ARM_R0: |
| 378 | *value = context_->uc_mcontext.arm_r0; |
| 379 | break; |
| 380 | case UNW_ARM_R1: |
| 381 | *value = context_->uc_mcontext.arm_r1; |
| 382 | break; |
| 383 | case UNW_ARM_R2: |
| 384 | *value = context_->uc_mcontext.arm_r2; |
| 385 | break; |
| 386 | case UNW_ARM_R3: |
| 387 | *value = context_->uc_mcontext.arm_r3; |
| 388 | break; |
| 389 | case UNW_ARM_R4: |
| 390 | *value = context_->uc_mcontext.arm_r4; |
| 391 | break; |
| 392 | case UNW_ARM_R5: |
| 393 | *value = context_->uc_mcontext.arm_r5; |
| 394 | break; |
| 395 | case UNW_ARM_R6: |
| 396 | *value = context_->uc_mcontext.arm_r6; |
| 397 | break; |
| 398 | case UNW_ARM_R7: |
| 399 | *value = context_->uc_mcontext.arm_r7; |
| 400 | break; |
| 401 | case UNW_ARM_R8: |
| 402 | *value = context_->uc_mcontext.arm_r8; |
| 403 | break; |
| 404 | case UNW_ARM_R9: |
| 405 | *value = context_->uc_mcontext.arm_r9; |
| 406 | break; |
| 407 | case UNW_ARM_R10: |
| 408 | *value = context_->uc_mcontext.arm_r10; |
| 409 | break; |
| 410 | case UNW_ARM_R11: |
| 411 | *value = context_->uc_mcontext.arm_fp; |
| 412 | break; |
| 413 | case UNW_ARM_R12: |
| 414 | *value = context_->uc_mcontext.arm_ip; |
| 415 | break; |
| 416 | case UNW_ARM_R13: |
| 417 | *value = context_->uc_mcontext.arm_sp; |
| 418 | break; |
| 419 | case UNW_ARM_R14: |
| 420 | *value = context_->uc_mcontext.arm_lr; |
| 421 | break; |
| 422 | case UNW_ARM_R15: |
| 423 | *value = context_->uc_mcontext.arm_pc; |
| 424 | break; |
| 425 | default: |
| 426 | result = false; |
| 427 | } |
| 428 | #elif defined(__aarch64__) |
| 429 | if (reg <= UNW_AARCH64_PC) { |
| 430 | *value = context_->uc_mcontext.regs[reg]; |
| 431 | } else { |
| 432 | result = false; |
| 433 | } |
| 434 | #elif defined(__x86_64__) |
| 435 | switch (reg) { |
| 436 | case UNW_X86_64_R8: |
| 437 | *value = context_->uc_mcontext.gregs[REG_R8]; |
| 438 | break; |
| 439 | case UNW_X86_64_R9: |
| 440 | *value = context_->uc_mcontext.gregs[REG_R9]; |
| 441 | break; |
| 442 | case UNW_X86_64_R10: |
| 443 | *value = context_->uc_mcontext.gregs[REG_R10]; |
| 444 | break; |
| 445 | case UNW_X86_64_R11: |
| 446 | *value = context_->uc_mcontext.gregs[REG_R11]; |
| 447 | break; |
| 448 | case UNW_X86_64_R12: |
| 449 | *value = context_->uc_mcontext.gregs[REG_R12]; |
| 450 | break; |
| 451 | case UNW_X86_64_R13: |
| 452 | *value = context_->uc_mcontext.gregs[REG_R13]; |
| 453 | break; |
| 454 | case UNW_X86_64_R14: |
| 455 | *value = context_->uc_mcontext.gregs[REG_R14]; |
| 456 | break; |
| 457 | case UNW_X86_64_R15: |
| 458 | *value = context_->uc_mcontext.gregs[REG_R15]; |
| 459 | break; |
| 460 | case UNW_X86_64_RDI: |
| 461 | *value = context_->uc_mcontext.gregs[REG_RDI]; |
| 462 | break; |
| 463 | case UNW_X86_64_RSI: |
| 464 | *value = context_->uc_mcontext.gregs[REG_RSI]; |
| 465 | break; |
| 466 | case UNW_X86_64_RBP: |
| 467 | *value = context_->uc_mcontext.gregs[REG_RBP]; |
| 468 | break; |
| 469 | case UNW_X86_64_RBX: |
| 470 | *value = context_->uc_mcontext.gregs[REG_RBX]; |
| 471 | break; |
| 472 | case UNW_X86_64_RDX: |
| 473 | *value = context_->uc_mcontext.gregs[REG_RDX]; |
| 474 | break; |
| 475 | case UNW_X86_64_RAX: |
| 476 | *value = context_->uc_mcontext.gregs[REG_RAX]; |
| 477 | break; |
| 478 | case UNW_X86_64_RCX: |
| 479 | *value = context_->uc_mcontext.gregs[REG_RCX]; |
| 480 | break; |
| 481 | case UNW_X86_64_RSP: |
| 482 | *value = context_->uc_mcontext.gregs[REG_RSP]; |
| 483 | break; |
| 484 | case UNW_X86_64_RIP: |
| 485 | *value = context_->uc_mcontext.gregs[REG_RIP]; |
| 486 | break; |
| 487 | default: |
| 488 | result = false; |
| 489 | } |
| 490 | #elif defined(__i386__) |
| 491 | switch (reg) { |
| 492 | case UNW_X86_GS: |
| 493 | *value = context_->uc_mcontext.gregs[REG_GS]; |
| 494 | break; |
| 495 | case UNW_X86_FS: |
| 496 | *value = context_->uc_mcontext.gregs[REG_FS]; |
| 497 | break; |
| 498 | case UNW_X86_ES: |
| 499 | *value = context_->uc_mcontext.gregs[REG_ES]; |
| 500 | break; |
| 501 | case UNW_X86_DS: |
| 502 | *value = context_->uc_mcontext.gregs[REG_DS]; |
| 503 | break; |
| 504 | case UNW_X86_EAX: |
| 505 | *value = context_->uc_mcontext.gregs[REG_EAX]; |
| 506 | break; |
| 507 | case UNW_X86_EBX: |
| 508 | *value = context_->uc_mcontext.gregs[REG_EBX]; |
| 509 | break; |
| 510 | case UNW_X86_ECX: |
| 511 | *value = context_->uc_mcontext.gregs[REG_ECX]; |
| 512 | break; |
| 513 | case UNW_X86_EDX: |
| 514 | *value = context_->uc_mcontext.gregs[REG_EDX]; |
| 515 | break; |
| 516 | case UNW_X86_ESI: |
| 517 | *value = context_->uc_mcontext.gregs[REG_ESI]; |
| 518 | break; |
| 519 | case UNW_X86_EDI: |
| 520 | *value = context_->uc_mcontext.gregs[REG_EDI]; |
| 521 | break; |
| 522 | case UNW_X86_EBP: |
| 523 | *value = context_->uc_mcontext.gregs[REG_EBP]; |
| 524 | break; |
| 525 | case UNW_X86_EIP: |
| 526 | *value = context_->uc_mcontext.gregs[REG_EIP]; |
| 527 | break; |
| 528 | case UNW_X86_ESP: |
| 529 | *value = context_->uc_mcontext.gregs[REG_ESP]; |
| 530 | break; |
| 531 | case UNW_X86_TRAPNO: |
| 532 | *value = context_->uc_mcontext.gregs[REG_TRAPNO]; |
| 533 | break; |
| 534 | case UNW_X86_CS: |
| 535 | *value = context_->uc_mcontext.gregs[REG_CS]; |
| 536 | break; |
| 537 | case UNW_X86_EFLAGS: |
| 538 | *value = context_->uc_mcontext.gregs[REG_EFL]; |
| 539 | break; |
| 540 | case UNW_X86_SS: |
| 541 | *value = context_->uc_mcontext.gregs[REG_SS]; |
| 542 | break; |
| 543 | default: |
| 544 | result = false; |
| 545 | } |
Colin Cross | 06d3149 | 2016-12-15 12:55:03 -0800 | [diff] [blame] | 546 | #else |
| 547 | UNUSED(reg); |
| 548 | UNUSED(value); |
| 549 | result = false; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 550 | #endif |
| 551 | return result; |
| 552 | } |
| 553 | |
| 554 | std::string BacktraceOffline::GetFunctionNameRaw(uintptr_t, uintptr_t* offset) { |
| 555 | // We don't have enough information to support this. And it is expensive. |
| 556 | *offset = 0; |
| 557 | return ""; |
| 558 | } |
| 559 | |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 560 | static DebugFrameInfo* ReadDebugFrameFromFile(const std::string& filename); |
| 561 | |
| 562 | DebugFrameInfo* BacktraceOffline::GetDebugFrameInFile(const std::string& filename) { |
| 563 | if (cache_file_) { |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 564 | auto it = g_debug_frames.find(filename); |
| 565 | if (it != g_debug_frames.end()) { |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 566 | return it->second.get(); |
| 567 | } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 568 | } |
| 569 | DebugFrameInfo* debug_frame = ReadDebugFrameFromFile(filename); |
| 570 | if (cache_file_) { |
Yabin Cui | c4a480e | 2017-02-02 15:25:08 -0800 | [diff] [blame^] | 571 | g_debug_frames.emplace(filename, std::unique_ptr<DebugFrameInfo>(debug_frame)); |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 572 | } |
| 573 | return debug_frame; |
| 574 | } |
| 575 | |
| 576 | static bool OmitEncodedValue(uint8_t encode, const uint8_t*& p) { |
| 577 | if (encode == DW_EH_PE_omit) { |
| 578 | return 0; |
| 579 | } |
| 580 | uint8_t format = encode & 0x0f; |
| 581 | switch (format) { |
| 582 | case DW_EH_PE_ptr: |
| 583 | p += sizeof(unw_word_t); |
| 584 | break; |
| 585 | case DW_EH_PE_uleb128: |
| 586 | case DW_EH_PE_sleb128: |
| 587 | while ((*p & 0x80) != 0) { |
| 588 | ++p; |
| 589 | } |
| 590 | ++p; |
| 591 | break; |
| 592 | case DW_EH_PE_udata2: |
| 593 | case DW_EH_PE_sdata2: |
| 594 | p += 2; |
| 595 | break; |
| 596 | case DW_EH_PE_udata4: |
| 597 | case DW_EH_PE_sdata4: |
| 598 | p += 4; |
| 599 | break; |
| 600 | case DW_EH_PE_udata8: |
| 601 | case DW_EH_PE_sdata8: |
| 602 | p += 8; |
| 603 | break; |
| 604 | default: |
| 605 | return false; |
| 606 | } |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | static bool GetFdeTableOffsetInEhFrameHdr(const std::vector<uint8_t>& data, |
| 611 | uint64_t* table_offset_in_eh_frame_hdr) { |
| 612 | const uint8_t* p = data.data(); |
| 613 | const uint8_t* end = p + data.size(); |
| 614 | if (p + 4 > end) { |
| 615 | return false; |
| 616 | } |
| 617 | uint8_t version = *p++; |
| 618 | if (version != 1) { |
| 619 | return false; |
| 620 | } |
| 621 | uint8_t eh_frame_ptr_encode = *p++; |
| 622 | uint8_t fde_count_encode = *p++; |
| 623 | uint8_t fde_table_encode = *p++; |
| 624 | |
| 625 | if (fde_table_encode != (DW_EH_PE_datarel | DW_EH_PE_sdata4)) { |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | if (!OmitEncodedValue(eh_frame_ptr_encode, p) || !OmitEncodedValue(fde_count_encode, p)) { |
| 630 | return false; |
| 631 | } |
| 632 | if (p >= end) { |
| 633 | return false; |
| 634 | } |
| 635 | *table_offset_in_eh_frame_hdr = p - data.data(); |
| 636 | return true; |
| 637 | } |
| 638 | |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 639 | template <class ELFT> |
| 640 | DebugFrameInfo* ReadDebugFrameFromELFFile(const llvm::object::ELFFile<ELFT>* elf) { |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 641 | DebugFrameInfo* result = new DebugFrameInfo; |
| 642 | result->text_end_vaddr = std::numeric_limits<uint64_t>::max(); |
| 643 | |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 644 | bool has_eh_frame_hdr = false; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 645 | bool has_eh_frame = false; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 646 | |
Stephen Hines | 19c30e9 | 2016-03-08 01:23:43 -0800 | [diff] [blame] | 647 | for (auto it = elf->section_begin(); it != elf->section_end(); ++it) { |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 648 | llvm::ErrorOr<llvm::StringRef> name = elf->getSectionName(&*it); |
| 649 | if (name) { |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 650 | std::string s = name.get(); |
| 651 | if (s == ".debug_frame") { |
| 652 | result->has_debug_frame = true; |
| 653 | } else if (s == ".gnu_debugdata") { |
| 654 | result->has_gnu_debugdata = true; |
| 655 | } else if (s == ".eh_frame_hdr") { |
| 656 | result->eh_frame.hdr_vaddr = it->sh_addr; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 657 | llvm::ErrorOr<llvm::ArrayRef<uint8_t>> data = elf->getSectionContents(&*it); |
| 658 | if (data) { |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 659 | result->eh_frame.hdr_data.insert(result->eh_frame.hdr_data.end(), |
| 660 | data->data(), data->data() + data->size()); |
| 661 | |
| 662 | uint64_t fde_table_offset; |
| 663 | if (GetFdeTableOffsetInEhFrameHdr(result->eh_frame.hdr_data, |
| 664 | &fde_table_offset)) { |
| 665 | result->eh_frame.fde_table_offset = fde_table_offset; |
| 666 | // Make sure we have at least one entry in fde_table. |
| 667 | if (fde_table_offset + 2 * sizeof(int32_t) <= data->size()) { |
| 668 | intptr_t eh_frame_hdr_vaddr = it->sh_addr; |
| 669 | int32_t sdata; |
| 670 | uint8_t* p = result->eh_frame.hdr_data.data() + fde_table_offset; |
| 671 | memcpy(&sdata, p, sizeof(sdata)); |
| 672 | result->eh_frame.min_func_vaddr = eh_frame_hdr_vaddr + sdata; |
| 673 | has_eh_frame_hdr = true; |
| 674 | } |
| 675 | } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 676 | } |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 677 | } else if (s == ".eh_frame") { |
| 678 | result->eh_frame.vaddr = it->sh_addr; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 679 | llvm::ErrorOr<llvm::ArrayRef<uint8_t>> data = elf->getSectionContents(&*it); |
| 680 | if (data) { |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 681 | result->eh_frame.data.insert(result->eh_frame.data.end(), |
| 682 | data->data(), data->data() + data->size()); |
| 683 | has_eh_frame = true; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 684 | } |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 685 | } else if (s == ".ARM.exidx") { |
| 686 | result->arm_exidx.exidx_vaddr = it->sh_addr; |
| 687 | llvm::ErrorOr<llvm::ArrayRef<uint8_t>> data = elf->getSectionContents(&*it); |
| 688 | if (data) { |
| 689 | size_t entry_count = data->size() / sizeof(ArmIdxEntry); |
| 690 | result->arm_exidx.exidx_data.resize(entry_count); |
| 691 | memcpy(result->arm_exidx.exidx_data.data(), data->data(), |
| 692 | entry_count * sizeof(ArmIdxEntry)); |
| 693 | if (entry_count > 0u) { |
| 694 | // Change IdxEntry.func_offset into vaddr. |
| 695 | result->arm_exidx.func_vaddr_array.reserve(entry_count); |
| 696 | uint32_t vaddr = it->sh_addr; |
| 697 | for (auto& entry : result->arm_exidx.exidx_data) { |
| 698 | uint32_t func_offset = entry.func_offset + vaddr; |
| 699 | // Clear bit 31 for the prel31 offset. |
| 700 | // Arm sets bit 0 to mark it as thumb code, remove the flag. |
| 701 | result->arm_exidx.func_vaddr_array.push_back( |
| 702 | func_offset & 0x7ffffffe); |
| 703 | vaddr += 8; |
| 704 | } |
| 705 | result->has_arm_exidx = true; |
| 706 | } |
| 707 | } |
| 708 | } else if (s == ".ARM.extab") { |
| 709 | result->arm_exidx.extab_vaddr = it->sh_addr; |
| 710 | llvm::ErrorOr<llvm::ArrayRef<uint8_t>> data = elf->getSectionContents(&*it); |
| 711 | if (data) { |
| 712 | result->arm_exidx.extab_data.insert(result->arm_exidx.extab_data.end(), |
| 713 | data->data(), data->data() + data->size()); |
| 714 | } |
| 715 | } else if (s == ".text") { |
| 716 | result->text_end_vaddr = it->sh_addr + it->sh_size; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 720 | |
| 721 | if (has_eh_frame_hdr && has_eh_frame) { |
| 722 | result->has_eh_frame = true; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 725 | result->min_vaddr = std::numeric_limits<uint64_t>::max(); |
Stephen Hines | 19c30e9 | 2016-03-08 01:23:43 -0800 | [diff] [blame] | 726 | for (auto it = elf->program_header_begin(); it != elf->program_header_end(); ++it) { |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 727 | if ((it->p_type == llvm::ELF::PT_LOAD) && (it->p_flags & llvm::ELF::PF_X)) { |
| 728 | if (it->p_vaddr < result->min_vaddr) { |
| 729 | result->min_vaddr = it->p_vaddr; |
| 730 | } |
| 731 | } |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 732 | } |
Yabin Cui | 5d991bc | 2016-11-15 17:47:09 -0800 | [diff] [blame] | 733 | if (!result->has_eh_frame && !result->has_arm_exidx && !result->has_debug_frame && |
| 734 | !result->has_gnu_debugdata) { |
| 735 | delete result; |
| 736 | return nullptr; |
| 737 | } |
| 738 | return result; |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Yabin Cui | 2ad59db | 2015-12-08 18:43:00 -0800 | [diff] [blame] | 741 | static bool IsValidElfPath(const std::string& filename) { |
| 742 | static const char elf_magic[] = {0x7f, 'E', 'L', 'F'}; |
| 743 | |
| 744 | struct stat st; |
| 745 | if (stat(filename.c_str(), &st) != 0 || !S_ISREG(st.st_mode)) { |
| 746 | return false; |
| 747 | } |
| 748 | FILE* fp = fopen(filename.c_str(), "reb"); |
| 749 | if (fp == nullptr) { |
| 750 | return false; |
| 751 | } |
| 752 | char buf[4]; |
| 753 | if (fread(buf, 4, 1, fp) != 1) { |
| 754 | fclose(fp); |
| 755 | return false; |
| 756 | } |
| 757 | fclose(fp); |
| 758 | return memcmp(buf, elf_magic, 4) == 0; |
| 759 | } |
| 760 | |
Yabin Cui | b791a76 | 2016-03-18 18:46:08 -0700 | [diff] [blame] | 761 | static bool IsValidApkPath(const std::string& apk_path) { |
| 762 | static const char zip_preamble[] = {0x50, 0x4b, 0x03, 0x04}; |
| 763 | struct stat st; |
| 764 | if (stat(apk_path.c_str(), &st) != 0 || !S_ISREG(st.st_mode)) { |
| 765 | return false; |
| 766 | } |
| 767 | FILE* fp = fopen(apk_path.c_str(), "reb"); |
| 768 | if (fp == nullptr) { |
| 769 | return false; |
| 770 | } |
| 771 | char buf[4]; |
| 772 | if (fread(buf, 4, 1, fp) != 1) { |
| 773 | fclose(fp); |
| 774 | return false; |
| 775 | } |
| 776 | fclose(fp); |
| 777 | return memcmp(buf, zip_preamble, 4) == 0; |
| 778 | } |
| 779 | |
| 780 | class ScopedZiparchiveHandle { |
| 781 | public: |
Chih-Hung Hsieh | 1c563d9 | 2016-04-29 15:44:04 -0700 | [diff] [blame] | 782 | explicit ScopedZiparchiveHandle(ZipArchiveHandle handle) : handle_(handle) { |
Yabin Cui | b791a76 | 2016-03-18 18:46:08 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | ~ScopedZiparchiveHandle() { |
| 786 | CloseArchive(handle_); |
| 787 | } |
| 788 | |
| 789 | private: |
| 790 | ZipArchiveHandle handle_; |
| 791 | }; |
| 792 | |
| 793 | llvm::object::OwningBinary<llvm::object::Binary> OpenEmbeddedElfFile(const std::string& filename) { |
| 794 | llvm::object::OwningBinary<llvm::object::Binary> nothing; |
| 795 | size_t pos = filename.find("!/"); |
| 796 | if (pos == std::string::npos) { |
| 797 | return nothing; |
| 798 | } |
| 799 | std::string apk_file = filename.substr(0, pos); |
| 800 | std::string elf_file = filename.substr(pos + 2); |
| 801 | if (!IsValidApkPath(apk_file)) { |
| 802 | BACK_LOGW("%s is not a valid apk file", apk_file.c_str()); |
| 803 | return nothing; |
| 804 | } |
| 805 | ZipArchiveHandle handle; |
| 806 | int32_t ret_code = OpenArchive(apk_file.c_str(), &handle); |
| 807 | if (ret_code != 0) { |
| 808 | CloseArchive(handle); |
| 809 | BACK_LOGW("failed to open archive %s: %s", apk_file.c_str(), ErrorCodeString(ret_code)); |
| 810 | return nothing; |
| 811 | } |
| 812 | ScopedZiparchiveHandle scoped_handle(handle); |
| 813 | ZipEntry zentry; |
| 814 | ret_code = FindEntry(handle, ZipString(elf_file.c_str()), &zentry); |
| 815 | if (ret_code != 0) { |
| 816 | BACK_LOGW("failed to find %s in %s: %s", elf_file.c_str(), apk_file.c_str(), |
| 817 | ErrorCodeString(ret_code)); |
| 818 | return nothing; |
| 819 | } |
| 820 | if (zentry.method != kCompressStored || zentry.compressed_length != zentry.uncompressed_length) { |
| 821 | BACK_LOGW("%s is compressed in %s, which doesn't support running directly", elf_file.c_str(), |
| 822 | apk_file.c_str()); |
| 823 | return nothing; |
| 824 | } |
| 825 | auto buffer_or_err = llvm::MemoryBuffer::getOpenFileSlice(GetFileDescriptor(handle), apk_file, |
| 826 | zentry.uncompressed_length, |
| 827 | zentry.offset); |
| 828 | if (!buffer_or_err) { |
| 829 | BACK_LOGW("failed to read %s in %s: %s", elf_file.c_str(), apk_file.c_str(), |
| 830 | buffer_or_err.getError().message().c_str()); |
| 831 | return nothing; |
| 832 | } |
| 833 | auto binary_or_err = llvm::object::createBinary(buffer_or_err.get()->getMemBufferRef()); |
| 834 | if (!binary_or_err) { |
| 835 | BACK_LOGW("failed to create binary for %s in %s: %s", elf_file.c_str(), apk_file.c_str(), |
Pirama Arumuga Nainar | 80fb4b0 | 2016-09-16 16:51:13 -0700 | [diff] [blame] | 836 | llvm::toString(binary_or_err.takeError()).c_str()); |
Yabin Cui | b791a76 | 2016-03-18 18:46:08 -0700 | [diff] [blame] | 837 | return nothing; |
| 838 | } |
| 839 | return llvm::object::OwningBinary<llvm::object::Binary>(std::move(binary_or_err.get()), |
| 840 | std::move(buffer_or_err.get())); |
| 841 | } |
| 842 | |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 843 | static DebugFrameInfo* ReadDebugFrameFromFile(const std::string& filename) { |
Yabin Cui | b791a76 | 2016-03-18 18:46:08 -0700 | [diff] [blame] | 844 | llvm::object::OwningBinary<llvm::object::Binary> owning_binary; |
| 845 | if (filename.find("!/") != std::string::npos) { |
| 846 | owning_binary = OpenEmbeddedElfFile(filename); |
| 847 | } else { |
| 848 | if (!IsValidElfPath(filename)) { |
| 849 | return nullptr; |
| 850 | } |
| 851 | auto binary_or_err = llvm::object::createBinary(llvm::StringRef(filename)); |
| 852 | if (!binary_or_err) { |
| 853 | return nullptr; |
| 854 | } |
| 855 | owning_binary = std::move(binary_or_err.get()); |
Yabin Cui | 2ad59db | 2015-12-08 18:43:00 -0800 | [diff] [blame] | 856 | } |
Yabin Cui | b791a76 | 2016-03-18 18:46:08 -0700 | [diff] [blame] | 857 | llvm::object::Binary* binary = owning_binary.getBinary(); |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 858 | auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary); |
| 859 | if (obj == nullptr) { |
| 860 | return nullptr; |
| 861 | } |
| 862 | if (auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj)) { |
| 863 | return ReadDebugFrameFromELFFile(elf->getELFFile()); |
| 864 | } |
| 865 | if (auto elf = llvm::dyn_cast<llvm::object::ELF64LEObjectFile>(obj)) { |
| 866 | return ReadDebugFrameFromELFFile(elf->getELFFile()); |
| 867 | } |
| 868 | return nullptr; |
| 869 | } |
Christopher Ferris | 8540216 | 2016-01-25 16:17:48 -0800 | [diff] [blame] | 870 | |
| 871 | Backtrace* Backtrace::CreateOffline(pid_t pid, pid_t tid, BacktraceMap* map, |
| 872 | const backtrace_stackinfo_t& stack, bool cache_file) { |
| 873 | return new BacktraceOffline(pid, tid, map, stack, cache_file); |
| 874 | } |