Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -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 <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <backtrace/BacktraceMap.h> |
| 22 | #include <unwindstack/Elf.h> |
| 23 | #include <unwindstack/MapInfo.h> |
| 24 | #include <unwindstack/Maps.h> |
| 25 | |
| 26 | #include "UnwindStackMap.h" |
| 27 | |
| 28 | //------------------------------------------------------------------------- |
| 29 | UnwindStackMap::UnwindStackMap(pid_t pid) : BacktraceMap(pid) {} |
| 30 | |
| 31 | bool UnwindStackMap::Build() { |
| 32 | if (pid_ == 0) { |
| 33 | pid_ = getpid(); |
| 34 | stack_maps_.reset(new unwindstack::LocalMaps); |
| 35 | } else { |
| 36 | stack_maps_.reset(new unwindstack::RemoteMaps(pid_)); |
| 37 | } |
| 38 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 39 | // Create the process memory object. |
| 40 | process_memory_ = unwindstack::Memory::CreateProcessMemory(pid_); |
| 41 | |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 42 | if (!stack_maps_->Parse()) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | // Iterate through the maps and fill in the backtrace_map_t structure. |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 47 | for (auto* map_info : *stack_maps_) { |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 48 | backtrace_map_t map; |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 49 | map.start = map_info->start; |
| 50 | map.end = map_info->end; |
| 51 | map.offset = map_info->offset; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 52 | // Set to -1 so that it is demand loaded. |
| 53 | map.load_bias = static_cast<uintptr_t>(-1); |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 54 | map.flags = map_info->flags; |
| 55 | map.name = map_info->name; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 56 | |
| 57 | maps_.push_back(map); |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | void UnwindStackMap::FillIn(uintptr_t addr, backtrace_map_t* map) { |
| 64 | BacktraceMap::FillIn(addr, map); |
| 65 | if (map->load_bias != static_cast<uintptr_t>(-1)) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // Fill in the load_bias. |
| 70 | unwindstack::MapInfo* map_info = stack_maps_->Find(addr); |
| 71 | if (map_info == nullptr) { |
| 72 | return; |
| 73 | } |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 74 | map->load_bias = map_info->GetLoadBias(process_memory_); |
| 75 | } |
| 76 | |
| 77 | uint64_t UnwindStackMap::GetLoadBias(size_t index) { |
| 78 | if (index >= stack_maps_->Total()) { |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | unwindstack::MapInfo* map_info = stack_maps_->Get(index); |
| 83 | if (map_info == nullptr) { |
| 84 | return 0; |
| 85 | } |
| 86 | return map_info->GetLoadBias(process_memory_); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 89 | std::string UnwindStackMap::GetFunctionName(uintptr_t pc, uintptr_t* offset) { |
| 90 | *offset = 0; |
| 91 | unwindstack::Maps* maps = stack_maps(); |
| 92 | |
| 93 | // Get the map for this |
| 94 | unwindstack::MapInfo* map_info = maps->Find(pc); |
| 95 | if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) { |
| 96 | return ""; |
| 97 | } |
| 98 | |
| 99 | unwindstack::Elf* elf = map_info->GetElf(process_memory(), true); |
| 100 | |
| 101 | std::string name; |
| 102 | uint64_t func_offset; |
| 103 | if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) { |
| 104 | return ""; |
| 105 | } |
| 106 | *offset = func_offset; |
| 107 | return name; |
| 108 | } |
| 109 | |
Josh Gao | ebea0ef | 2017-09-06 15:39:14 -0700 | [diff] [blame] | 110 | std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() { |
| 111 | return process_memory_; |
| 112 | } |
| 113 | |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 114 | //------------------------------------------------------------------------- |
| 115 | // BacktraceMap create function. |
| 116 | //------------------------------------------------------------------------- |
Christopher Ferris | 086baf9 | 2017-10-17 14:12:52 -0700 | [diff] [blame] | 117 | BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) { |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 118 | BacktraceMap* map; |
| 119 | |
| 120 | if (uncached) { |
| 121 | // Force use of the base class to parse the maps when this call is made. |
| 122 | map = new BacktraceMap(pid); |
| 123 | } else if (pid == getpid()) { |
| 124 | map = new UnwindStackMap(0); |
| 125 | } else { |
| 126 | map = new UnwindStackMap(pid); |
| 127 | } |
| 128 | if (!map->Build()) { |
| 129 | delete map; |
| 130 | return nullptr; |
| 131 | } |
| 132 | return map; |
| 133 | } |