blob: 726fdfa043765c8b97600fc254590d78d9645a0f [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
Christopher Ferris150db122017-12-20 18:49:01 -080021#include <string>
22#include <vector>
23
Christopher Ferris6f3981c2017-07-27 09:29:18 -070024#include <backtrace/BacktraceMap.h>
25#include <unwindstack/Elf.h>
26#include <unwindstack/MapInfo.h>
27#include <unwindstack/Maps.h>
Christopher Ferris4568f4b2018-10-23 17:42:41 -070028#include <unwindstack/Regs.h>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070029
30#include "UnwindStackMap.h"
31
32//-------------------------------------------------------------------------
33UnwindStackMap::UnwindStackMap(pid_t pid) : BacktraceMap(pid) {}
34
35bool UnwindStackMap::Build() {
36 if (pid_ == 0) {
37 pid_ = getpid();
38 stack_maps_.reset(new unwindstack::LocalMaps);
39 } else {
40 stack_maps_.reset(new unwindstack::RemoteMaps(pid_));
41 }
42
Christopher Ferris5f118512017-09-01 11:17:16 -070043 // Create the process memory object.
44 process_memory_ = unwindstack::Memory::CreateProcessMemory(pid_);
45
Christopher Ferris6f3981c2017-07-27 09:29:18 -070046 if (!stack_maps_->Parse()) {
47 return false;
48 }
49
50 // Iterate through the maps and fill in the backtrace_map_t structure.
Florian Mayer3d67d342019-02-27 18:00:37 +000051 for (const auto& map_info : *stack_maps_) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070052 backtrace_map_t map;
Christopher Ferrisbe788d82017-11-27 14:50:38 -080053 map.start = map_info->start;
54 map.end = map_info->end;
55 map.offset = map_info->offset;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070056 // Set to -1 so that it is demand loaded.
Christopher Ferris7937a362018-01-18 11:15:49 -080057 map.load_bias = static_cast<uint64_t>(-1);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080058 map.flags = map_info->flags;
59 map.name = map_info->name;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070060
61 maps_.push_back(map);
62 }
63
64 return true;
65}
66
Christopher Ferris7937a362018-01-18 11:15:49 -080067void UnwindStackMap::FillIn(uint64_t addr, backtrace_map_t* map) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070068 BacktraceMap::FillIn(addr, map);
Christopher Ferris7937a362018-01-18 11:15:49 -080069 if (map->load_bias != static_cast<uint64_t>(-1)) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070070 return;
71 }
72
73 // Fill in the load_bias.
74 unwindstack::MapInfo* map_info = stack_maps_->Find(addr);
75 if (map_info == nullptr) {
76 return;
77 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080078 map->load_bias = map_info->GetLoadBias(process_memory_);
79}
80
81uint64_t UnwindStackMap::GetLoadBias(size_t index) {
82 if (index >= stack_maps_->Total()) {
83 return 0;
84 }
85
86 unwindstack::MapInfo* map_info = stack_maps_->Get(index);
87 if (map_info == nullptr) {
88 return 0;
89 }
90 return map_info->GetLoadBias(process_memory_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -070091}
92
Christopher Ferris7937a362018-01-18 11:15:49 -080093std::string UnwindStackMap::GetFunctionName(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -070094 *offset = 0;
95 unwindstack::Maps* maps = stack_maps();
96
97 // Get the map for this
98 unwindstack::MapInfo* map_info = maps->Find(pc);
99 if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) {
100 return "";
101 }
102
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700103 if (arch_ == unwindstack::ARCH_UNKNOWN) {
104 if (pid_ == getpid()) {
105 arch_ = unwindstack::Regs::CurrentArch();
106 } else {
107 // Create a remote regs, to figure out the architecture.
108 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::RemoteGet(pid_));
109 arch_ = regs->Arch();
110 }
111 }
112
113 unwindstack::Elf* elf = map_info->GetElf(process_memory(), arch_);
Josh Gao358de182017-09-06 22:16:09 -0700114
115 std::string name;
116 uint64_t func_offset;
117 if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) {
118 return "";
119 }
120 *offset = func_offset;
121 return name;
122}
123
Josh Gaoebea0ef2017-09-06 15:39:14 -0700124std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() {
125 return process_memory_;
126}
127
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800128UnwindStackOfflineMap::UnwindStackOfflineMap(pid_t pid) : UnwindStackMap(pid) {}
129
130bool UnwindStackOfflineMap::Build() {
131 return false;
132}
133
Christopher Ferris432981b2018-02-22 19:42:53 -0800134bool UnwindStackOfflineMap::Build(const std::vector<backtrace_map_t>& backtrace_maps) {
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800135 for (const backtrace_map_t& map : backtrace_maps) {
136 maps_.push_back(map);
137 }
138
139 std::sort(maps_.begin(), maps_.end(),
140 [](const backtrace_map_t& a, const backtrace_map_t& b) { return a.start < b.start; });
141
142 unwindstack::Maps* maps = new unwindstack::Maps;
143 stack_maps_.reset(maps);
144 for (const backtrace_map_t& map : maps_) {
145 maps->Add(map.start, map.end, map.offset, map.flags, map.name, map.load_bias);
146 }
Christopher Ferris432981b2018-02-22 19:42:53 -0800147 return true;
148}
149
150bool UnwindStackOfflineMap::CreateProcessMemory(const backtrace_stackinfo_t& stack) {
151 if (stack.start >= stack.end) {
152 return false;
153 }
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800154
155 // Create the process memory from the stack data.
Christopher Ferris6633b0c2018-04-03 16:03:28 -0700156 if (memory_ == nullptr) {
157 memory_ = new unwindstack::MemoryOfflineBuffer(stack.data, stack.start, stack.end);
158 process_memory_.reset(memory_);
159 } else {
160 memory_->Reset(stack.data, stack.start, stack.end);
161 }
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800162 return true;
163}
164
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700165//-------------------------------------------------------------------------
166// BacktraceMap create function.
167//-------------------------------------------------------------------------
Christopher Ferris086baf92017-10-17 14:12:52 -0700168BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700169 BacktraceMap* map;
170
171 if (uncached) {
172 // Force use of the base class to parse the maps when this call is made.
173 map = new BacktraceMap(pid);
174 } else if (pid == getpid()) {
175 map = new UnwindStackMap(0);
176 } else {
177 map = new UnwindStackMap(pid);
178 }
179 if (!map->Build()) {
180 delete map;
181 return nullptr;
182 }
183 return map;
184}
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800185
186//-------------------------------------------------------------------------
187// BacktraceMap create offline function.
188//-------------------------------------------------------------------------
Christopher Ferris432981b2018-02-22 19:42:53 -0800189BacktraceMap* BacktraceMap::CreateOffline(pid_t pid, const std::vector<backtrace_map_t>& maps) {
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800190 UnwindStackOfflineMap* map = new UnwindStackOfflineMap(pid);
Christopher Ferris432981b2018-02-22 19:42:53 -0800191 if (!map->Build(maps)) {
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800192 delete map;
193 return nullptr;
194 }
195 return map;
196}