blob: 836a774b99266fdeb8d5323945164c7c56641a86 [file] [log] [blame]
Christopher Ferris6f3981c2017-07-27 09:29:18 -07001/*
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//-------------------------------------------------------------------------
29UnwindStackMap::UnwindStackMap(pid_t pid) : BacktraceMap(pid) {}
30
31bool 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 Ferris5f118512017-09-01 11:17:16 -070039 // Create the process memory object.
40 process_memory_ = unwindstack::Memory::CreateProcessMemory(pid_);
41
Christopher Ferris6f3981c2017-07-27 09:29:18 -070042 if (!stack_maps_->Parse()) {
43 return false;
44 }
45
46 // Iterate through the maps and fill in the backtrace_map_t structure.
Christopher Ferrisbe788d82017-11-27 14:50:38 -080047 for (auto* map_info : *stack_maps_) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070048 backtrace_map_t map;
Christopher Ferrisbe788d82017-11-27 14:50:38 -080049 map.start = map_info->start;
50 map.end = map_info->end;
51 map.offset = map_info->offset;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070052 // Set to -1 so that it is demand loaded.
53 map.load_bias = static_cast<uintptr_t>(-1);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080054 map.flags = map_info->flags;
55 map.name = map_info->name;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070056
57 maps_.push_back(map);
58 }
59
60 return true;
61}
62
63void 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 Ferrisb7de5f52017-12-01 21:37:37 -080074 map->load_bias = map_info->GetLoadBias(process_memory_);
75}
76
77uint64_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 Ferris6f3981c2017-07-27 09:29:18 -070087}
88
Josh Gao358de182017-09-06 22:16:09 -070089std::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 Gaoebea0ef2017-09-06 15:39:14 -0700110std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() {
111 return process_memory_;
112}
113
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700114//-------------------------------------------------------------------------
115// BacktraceMap create function.
116//-------------------------------------------------------------------------
Christopher Ferris086baf92017-10-17 14:12:52 -0700117BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700118 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}