| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -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 |  | 
|  | 17 | #include <elf.h> | 
|  | 18 | #include <inttypes.h> | 
|  | 19 | #include <stdint.h> | 
|  | 20 | #include <sys/types.h> | 
|  | 21 | #include <unistd.h> | 
|  | 22 |  | 
|  | 23 | #include <android-base/stringprintf.h> | 
|  | 24 |  | 
|  | 25 | #include <unwindstack/Elf.h> | 
|  | 26 | #include <unwindstack/MapInfo.h> | 
|  | 27 | #include <unwindstack/Unwinder.h> | 
|  | 28 |  | 
|  | 29 | namespace unwindstack { | 
|  | 30 |  | 
|  | 31 | void Unwinder::FillInFrame(MapInfo* map_info, uint64_t* rel_pc) { | 
|  | 32 | size_t frame_num = frames_.size(); | 
|  | 33 | frames_.resize(frame_num + 1); | 
|  | 34 | FrameData* frame = &frames_.at(frame_num); | 
|  | 35 | frame->num = frame_num; | 
|  | 36 | frame->pc = regs_->pc(); | 
|  | 37 | frame->sp = regs_->sp(); | 
|  | 38 | frame->rel_pc = frame->pc; | 
|  | 39 |  | 
|  | 40 | if (map_info == nullptr) { | 
|  | 41 | return; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | Elf* elf = map_info->GetElf(process_memory_, true); | 
|  | 45 | *rel_pc = elf->GetRelPc(regs_->pc(), map_info); | 
|  | 46 | if (frame_num != 0) { | 
|  | 47 | // Don't adjust the first frame pc. | 
|  | 48 | frame->rel_pc = regs_->GetAdjustedPc(*rel_pc, elf); | 
|  | 49 |  | 
|  | 50 | // Adjust the original pc. | 
|  | 51 | frame->pc -= *rel_pc - frame->rel_pc; | 
|  | 52 | } else { | 
|  | 53 | frame->rel_pc = *rel_pc; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | frame->map_name = map_info->name; | 
|  | 57 | frame->map_offset = map_info->elf_offset; | 
|  | 58 | frame->map_start = map_info->start; | 
|  | 59 | frame->map_end = map_info->end; | 
|  | 60 |  | 
|  | 61 | if (!elf->GetFunctionName(frame->rel_pc, &frame->function_name, &frame->function_offset)) { | 
|  | 62 | frame->function_name = ""; | 
|  | 63 | frame->function_offset = 0; | 
|  | 64 | } | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | void Unwinder::Unwind() { | 
|  | 68 | frames_.clear(); | 
|  | 69 |  | 
|  | 70 | bool return_address_attempt = false; | 
|  | 71 | for (; frames_.size() < max_frames_;) { | 
|  | 72 | MapInfo* map_info = maps_->Find(regs_->pc()); | 
|  | 73 |  | 
|  | 74 | uint64_t rel_pc; | 
|  | 75 | FillInFrame(map_info, &rel_pc); | 
|  | 76 |  | 
|  | 77 | bool stepped; | 
|  | 78 | if (map_info == nullptr) { | 
|  | 79 | stepped = false; | 
|  | 80 | } else { | 
|  | 81 | bool finished; | 
|  | 82 | stepped = map_info->elf->Step(rel_pc + map_info->elf_offset, regs_, process_memory_.get(), | 
|  | 83 | &finished); | 
|  | 84 | if (stepped && finished) { | 
|  | 85 | break; | 
|  | 86 | } | 
|  | 87 | } | 
|  | 88 | if (!stepped) { | 
|  | 89 | if (return_address_attempt) { | 
|  | 90 | // Remove the speculative frame. | 
|  | 91 | frames_.pop_back(); | 
|  | 92 | break; | 
|  | 93 | } else { | 
|  | 94 | // Steping didn't work, try this secondary method. | 
|  | 95 | if (!regs_->SetPcFromReturnAddress(process_memory_.get())) { | 
|  | 96 | break; | 
|  | 97 | } | 
|  | 98 | return_address_attempt = true; | 
|  | 99 | } | 
|  | 100 | } else { | 
|  | 101 | return_address_attempt = false; | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | std::string Unwinder::FormatFrame(size_t frame_num) { | 
|  | 107 | if (frame_num >= frames_.size()) { | 
|  | 108 | return ""; | 
|  | 109 | } | 
|  | 110 | return FormatFrame(frames_[frame_num], | 
|  | 111 | regs_->MachineType() == EM_ARM || regs_->MachineType() == EM_386); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | std::string Unwinder::FormatFrame(const FrameData& frame, bool bits32) { | 
|  | 115 | std::string data; | 
|  | 116 |  | 
|  | 117 | if (bits32) { | 
|  | 118 | data += android::base::StringPrintf("  #%02zu pc %08" PRIx64, frame.num, frame.rel_pc); | 
|  | 119 | } else { | 
|  | 120 | data += android::base::StringPrintf("  #%02zu pc %016" PRIx64, frame.num, frame.rel_pc); | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | if (frame.map_offset != 0) { | 
|  | 124 | data += android::base::StringPrintf(" (offset 0x%" PRIx64 ")", frame.map_offset); | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | if (frame.map_start == frame.map_end) { | 
|  | 128 | // No valid map associated with this frame. | 
|  | 129 | data += "  <unknown>"; | 
|  | 130 | } else if (!frame.map_name.empty()) { | 
|  | 131 | data += "  " + frame.map_name; | 
|  | 132 | } else { | 
|  | 133 | data += android::base::StringPrintf("  <anonymous:%" PRIx64 ">", frame.map_start); | 
|  | 134 | } | 
|  | 135 | if (!frame.function_name.empty()) { | 
|  | 136 | data += " (" + frame.function_name; | 
|  | 137 | if (frame.function_offset != 0) { | 
|  | 138 | data += android::base::StringPrintf("+%" PRId64, frame.function_offset); | 
|  | 139 | } | 
|  | 140 | data += ')'; | 
|  | 141 | } | 
|  | 142 | return data; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | }  // namespace unwindstack |