| 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 | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 74 | unwindstack::Elf* elf = map_info->GetElf(process_memory_, true); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 75 | map->load_bias = elf->GetLoadBias(); | 
|  | 76 | } | 
|  | 77 |  | 
| Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 78 | std::string UnwindStackMap::GetFunctionName(uintptr_t pc, uintptr_t* offset) { | 
|  | 79 | *offset = 0; | 
|  | 80 | unwindstack::Maps* maps = stack_maps(); | 
|  | 81 |  | 
|  | 82 | // Get the map for this | 
|  | 83 | unwindstack::MapInfo* map_info = maps->Find(pc); | 
|  | 84 | if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) { | 
|  | 85 | return ""; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | unwindstack::Elf* elf = map_info->GetElf(process_memory(), true); | 
|  | 89 |  | 
|  | 90 | std::string name; | 
|  | 91 | uint64_t func_offset; | 
|  | 92 | if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) { | 
|  | 93 | return ""; | 
|  | 94 | } | 
|  | 95 | *offset = func_offset; | 
|  | 96 | return name; | 
|  | 97 | } | 
|  | 98 |  | 
| Josh Gao | ebea0ef | 2017-09-06 15:39:14 -0700 | [diff] [blame] | 99 | std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() { | 
|  | 100 | return process_memory_; | 
|  | 101 | } | 
|  | 102 |  | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 103 | //------------------------------------------------------------------------- | 
|  | 104 | // BacktraceMap create function. | 
|  | 105 | //------------------------------------------------------------------------- | 
| Christopher Ferris | 086baf9 | 2017-10-17 14:12:52 -0700 | [diff] [blame] | 106 | BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) { | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 107 | BacktraceMap* map; | 
|  | 108 |  | 
|  | 109 | if (uncached) { | 
|  | 110 | // Force use of the base class to parse the maps when this call is made. | 
|  | 111 | map = new BacktraceMap(pid); | 
|  | 112 | } else if (pid == getpid()) { | 
|  | 113 | map = new UnwindStackMap(0); | 
|  | 114 | } else { | 
|  | 115 | map = new UnwindStackMap(pid); | 
|  | 116 | } | 
|  | 117 | if (!map->Build()) { | 
|  | 118 | delete map; | 
|  | 119 | return nullptr; | 
|  | 120 | } | 
|  | 121 | return map; | 
|  | 122 | } |