Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <pthread.h> |
| 30 | #include <stdint.h> |
| 31 | |
| 32 | #include <memory> |
| 33 | #include <string> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include <unwindstack/Elf.h> |
| 37 | #include <unwindstack/LocalUnwinder.h> |
| 38 | #include <unwindstack/MapInfo.h> |
| 39 | #include <unwindstack/Maps.h> |
| 40 | #include <unwindstack/Memory.h> |
| 41 | #include <unwindstack/Regs.h> |
| 42 | #include <unwindstack/RegsGetLocal.h> |
| 43 | |
| 44 | namespace unwindstack { |
| 45 | |
| 46 | bool LocalUnwinder::Init() { |
| 47 | pthread_rwlock_init(&maps_rwlock_, nullptr); |
| 48 | |
| 49 | // Create the maps. |
| 50 | maps_.reset(new unwindstack::LocalUpdatableMaps()); |
| 51 | if (!maps_->Parse()) { |
| 52 | maps_.reset(); |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | process_memory_ = unwindstack::Memory::CreateProcessMemory(getpid()); |
| 57 | |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | bool LocalUnwinder::ShouldSkipLibrary(const std::string& map_name) { |
| 62 | for (const std::string& skip_library : skip_libraries_) { |
| 63 | if (skip_library == map_name) { |
| 64 | return true; |
| 65 | } |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | MapInfo* LocalUnwinder::GetMapInfo(uint64_t pc) { |
| 71 | pthread_rwlock_rdlock(&maps_rwlock_); |
| 72 | MapInfo* map_info = maps_->Find(pc); |
| 73 | pthread_rwlock_unlock(&maps_rwlock_); |
| 74 | |
| 75 | if (map_info == nullptr) { |
| 76 | pthread_rwlock_wrlock(&maps_rwlock_); |
| 77 | // This is guaranteed not to invalidate any previous MapInfo objects so |
| 78 | // we don't need to worry about any MapInfo* values already in use. |
| 79 | if (maps_->Reparse()) { |
| 80 | map_info = maps_->Find(pc); |
| 81 | } |
| 82 | pthread_rwlock_unlock(&maps_rwlock_); |
| 83 | } |
| 84 | |
| 85 | return map_info; |
| 86 | } |
| 87 | |
| 88 | bool LocalUnwinder::Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames) { |
| 89 | std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal()); |
| 90 | unwindstack::RegsGetLocal(regs.get()); |
Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 91 | ArchEnum arch = regs->Arch(); |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 92 | |
| 93 | size_t num_frames = 0; |
| 94 | bool adjust_pc = false; |
| 95 | while (true) { |
| 96 | uint64_t cur_pc = regs->pc(); |
| 97 | uint64_t cur_sp = regs->sp(); |
| 98 | |
| 99 | MapInfo* map_info = GetMapInfo(cur_pc); |
| 100 | if (map_info == nullptr) { |
| 101 | break; |
| 102 | } |
| 103 | |
Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 104 | Elf* elf = map_info->GetElf(process_memory_, arch); |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 105 | uint64_t rel_pc = elf->GetRelPc(cur_pc, map_info); |
| 106 | uint64_t step_pc = rel_pc; |
| 107 | uint64_t pc_adjustment; |
| 108 | if (adjust_pc) { |
| 109 | pc_adjustment = regs->GetPcAdjustment(rel_pc, elf); |
| 110 | } else { |
| 111 | pc_adjustment = 0; |
| 112 | } |
| 113 | step_pc -= pc_adjustment; |
Christopher Ferris | d11ed86 | 2019-04-11 19:45:35 -0700 | [diff] [blame] | 114 | |
| 115 | bool finished = false; |
| 116 | if (elf->StepIfSignalHandler(rel_pc, regs.get(), process_memory_.get())) { |
| 117 | step_pc = rel_pc; |
| 118 | } else if (!elf->Step(step_pc, regs.get(), process_memory_.get(), &finished)) { |
| 119 | finished = true; |
| 120 | } |
| 121 | |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 122 | // Skip any locations that are within this library. |
| 123 | if (num_frames != 0 || !ShouldSkipLibrary(map_info->name)) { |
| 124 | // Add frame information. |
| 125 | std::string func_name; |
| 126 | uint64_t func_offset; |
| 127 | if (elf->GetFunctionName(rel_pc, &func_name, &func_offset)) { |
| 128 | frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, |
| 129 | func_name, func_offset); |
| 130 | } else { |
| 131 | frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, "", 0); |
| 132 | } |
| 133 | num_frames++; |
| 134 | } |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 135 | |
Christopher Ferris | d11ed86 | 2019-04-11 19:45:35 -0700 | [diff] [blame] | 136 | if (finished || frame_info->size() == max_frames || |
| 137 | (cur_pc == regs->pc() && cur_sp == regs->sp())) { |
| 138 | break; |
| 139 | } |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 140 | adjust_pc = true; |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 141 | } |
| 142 | return num_frames != 0; |
| 143 | } |
| 144 | |
| 145 | } // namespace unwindstack |