| 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 |  | 
|  | 39 | if (!stack_maps_->Parse()) { | 
|  | 40 | return false; | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | // Iterate through the maps and fill in the backtrace_map_t structure. | 
|  | 44 | for (auto& map_info : *stack_maps_) { | 
|  | 45 | backtrace_map_t map; | 
|  | 46 | map.start = map_info.start; | 
|  | 47 | map.end = map_info.end; | 
|  | 48 | map.offset = map_info.offset; | 
|  | 49 | // Set to -1 so that it is demand loaded. | 
|  | 50 | map.load_bias = static_cast<uintptr_t>(-1); | 
|  | 51 | map.flags = map_info.flags; | 
|  | 52 | map.name = map_info.name; | 
|  | 53 |  | 
|  | 54 | maps_.push_back(map); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | return true; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | void UnwindStackMap::FillIn(uintptr_t addr, backtrace_map_t* map) { | 
|  | 61 | BacktraceMap::FillIn(addr, map); | 
|  | 62 | if (map->load_bias != static_cast<uintptr_t>(-1)) { | 
|  | 63 | return; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | // Fill in the load_bias. | 
|  | 67 | unwindstack::MapInfo* map_info = stack_maps_->Find(addr); | 
|  | 68 | if (map_info == nullptr) { | 
|  | 69 | return; | 
|  | 70 | } | 
|  | 71 | unwindstack::Elf* elf = map_info->GetElf(pid_, true); | 
|  | 72 | map->load_bias = elf->GetLoadBias(); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | //------------------------------------------------------------------------- | 
|  | 76 | // BacktraceMap create function. | 
|  | 77 | //------------------------------------------------------------------------- | 
|  | 78 | BacktraceMap* BacktraceMap::CreateNew(pid_t pid, bool uncached) { | 
|  | 79 | BacktraceMap* map; | 
|  | 80 |  | 
|  | 81 | if (uncached) { | 
|  | 82 | // Force use of the base class to parse the maps when this call is made. | 
|  | 83 | map = new BacktraceMap(pid); | 
|  | 84 | } else if (pid == getpid()) { | 
|  | 85 | map = new UnwindStackMap(0); | 
|  | 86 | } else { | 
|  | 87 | map = new UnwindStackMap(pid); | 
|  | 88 | } | 
|  | 89 | if (!map->Build()) { | 
|  | 90 | delete map; | 
|  | 91 | return nullptr; | 
|  | 92 | } | 
|  | 93 | return map; | 
|  | 94 | } |