blob: 97f8d782d1c2e559bcd54fedd8f19d6c291fc2aa [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>
28
Christopher Ferris0b06a592018-01-19 10:26:36 -080029#include "UnwindDexFile.h"
Christopher Ferris6f3981c2017-07-27 09:29:18 -070030#include "UnwindStackMap.h"
31
32//-------------------------------------------------------------------------
33UnwindStackMap::UnwindStackMap(pid_t pid) : BacktraceMap(pid) {}
34
Christopher Ferris0b06a592018-01-19 10:26:36 -080035UnwindStackMap::~UnwindStackMap() {
36#ifndef NO_LIBDEXFILE
37 for (auto& entry : dex_files_) {
38 delete entry.second;
39 }
40#endif
41}
42
Christopher Ferris6f3981c2017-07-27 09:29:18 -070043bool UnwindStackMap::Build() {
44 if (pid_ == 0) {
45 pid_ = getpid();
46 stack_maps_.reset(new unwindstack::LocalMaps);
47 } else {
48 stack_maps_.reset(new unwindstack::RemoteMaps(pid_));
49 }
50
Christopher Ferris5f118512017-09-01 11:17:16 -070051 // Create the process memory object.
52 process_memory_ = unwindstack::Memory::CreateProcessMemory(pid_);
53
Christopher Ferris150db122017-12-20 18:49:01 -080054 // Create a JitDebug object for getting jit unwind information.
55 std::vector<std::string> search_libs_{"libart.so", "libartd.so"};
56 jit_debug_.reset(new unwindstack::JitDebug(process_memory_, search_libs_));
57
Christopher Ferris6f3981c2017-07-27 09:29:18 -070058 if (!stack_maps_->Parse()) {
59 return false;
60 }
61
62 // Iterate through the maps and fill in the backtrace_map_t structure.
Christopher Ferrisbe788d82017-11-27 14:50:38 -080063 for (auto* map_info : *stack_maps_) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070064 backtrace_map_t map;
Christopher Ferrisbe788d82017-11-27 14:50:38 -080065 map.start = map_info->start;
66 map.end = map_info->end;
67 map.offset = map_info->offset;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070068 // Set to -1 so that it is demand loaded.
Christopher Ferris7937a362018-01-18 11:15:49 -080069 map.load_bias = static_cast<uint64_t>(-1);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080070 map.flags = map_info->flags;
71 map.name = map_info->name;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070072
73 maps_.push_back(map);
74 }
75
76 return true;
77}
78
Christopher Ferris7937a362018-01-18 11:15:49 -080079void UnwindStackMap::FillIn(uint64_t addr, backtrace_map_t* map) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070080 BacktraceMap::FillIn(addr, map);
Christopher Ferris7937a362018-01-18 11:15:49 -080081 if (map->load_bias != static_cast<uint64_t>(-1)) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070082 return;
83 }
84
85 // Fill in the load_bias.
86 unwindstack::MapInfo* map_info = stack_maps_->Find(addr);
87 if (map_info == nullptr) {
88 return;
89 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080090 map->load_bias = map_info->GetLoadBias(process_memory_);
91}
92
93uint64_t UnwindStackMap::GetLoadBias(size_t index) {
94 if (index >= stack_maps_->Total()) {
95 return 0;
96 }
97
98 unwindstack::MapInfo* map_info = stack_maps_->Get(index);
99 if (map_info == nullptr) {
100 return 0;
101 }
102 return map_info->GetLoadBias(process_memory_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700103}
104
Christopher Ferris7937a362018-01-18 11:15:49 -0800105std::string UnwindStackMap::GetFunctionName(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700106 *offset = 0;
107 unwindstack::Maps* maps = stack_maps();
108
109 // Get the map for this
110 unwindstack::MapInfo* map_info = maps->Find(pc);
111 if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) {
112 return "";
113 }
114
115 unwindstack::Elf* elf = map_info->GetElf(process_memory(), true);
116
117 std::string name;
118 uint64_t func_offset;
119 if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) {
120 return "";
121 }
122 *offset = func_offset;
123 return name;
124}
125
Josh Gaoebea0ef2017-09-06 15:39:14 -0700126std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() {
127 return process_memory_;
128}
129
Christopher Ferris0b06a592018-01-19 10:26:36 -0800130#ifdef NO_LIBDEXFILE
131UnwindDexFile* UnwindStackMap::GetDexFile(uint64_t, unwindstack::MapInfo*) {
132 return nullptr;
133}
134#else
135UnwindDexFile* UnwindStackMap::GetDexFile(uint64_t dex_file_offset, unwindstack::MapInfo* info) {
136 // Lock while we get the data.
137 std::lock_guard<std::mutex> guard(dex_lock_);
138 UnwindDexFile* dex_file;
139 auto entry = dex_files_.find(dex_file_offset);
140 if (entry == dex_files_.end()) {
141 dex_file = UnwindDexFile::Create(dex_file_offset, process_memory_.get(), info);
142 dex_files_[dex_file_offset] = dex_file;
143 } else {
144 dex_file = entry->second;
145 }
146 return dex_file;
147}
148#endif
149
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800150UnwindStackOfflineMap::UnwindStackOfflineMap(pid_t pid) : UnwindStackMap(pid) {}
151
152bool UnwindStackOfflineMap::Build() {
153 return false;
154}
155
156bool UnwindStackOfflineMap::Build(const std::vector<backtrace_map_t>& backtrace_maps,
157 const backtrace_stackinfo_t& stack) {
158 if (stack.start >= stack.end) {
159 return false;
160 }
161
162 for (const backtrace_map_t& map : backtrace_maps) {
163 maps_.push_back(map);
164 }
165
166 std::sort(maps_.begin(), maps_.end(),
167 [](const backtrace_map_t& a, const backtrace_map_t& b) { return a.start < b.start; });
168
169 unwindstack::Maps* maps = new unwindstack::Maps;
170 stack_maps_.reset(maps);
171 for (const backtrace_map_t& map : maps_) {
172 maps->Add(map.start, map.end, map.offset, map.flags, map.name, map.load_bias);
173 }
174
175 // Create the process memory from the stack data.
176 uint64_t size = stack.end - stack.start;
177 unwindstack::MemoryBuffer* memory = new unwindstack::MemoryBuffer;
178 memory->Resize(size);
179 memcpy(memory->GetPtr(0), stack.data, size);
180 std::shared_ptr<unwindstack::Memory> shared_memory(memory);
181
182 process_memory_.reset(new unwindstack::MemoryRange(shared_memory, 0, size, stack.start));
183
184 return true;
185}
186
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700187//-------------------------------------------------------------------------
188// BacktraceMap create function.
189//-------------------------------------------------------------------------
Christopher Ferris086baf92017-10-17 14:12:52 -0700190BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700191 BacktraceMap* map;
192
193 if (uncached) {
194 // Force use of the base class to parse the maps when this call is made.
195 map = new BacktraceMap(pid);
196 } else if (pid == getpid()) {
197 map = new UnwindStackMap(0);
198 } else {
199 map = new UnwindStackMap(pid);
200 }
201 if (!map->Build()) {
202 delete map;
203 return nullptr;
204 }
205 return map;
206}
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800207
208//-------------------------------------------------------------------------
209// BacktraceMap create offline function.
210//-------------------------------------------------------------------------
211BacktraceMap* BacktraceMap::CreateOffline(pid_t pid, const std::vector<backtrace_map_t>& maps,
212 const backtrace_stackinfo_t& stack) {
213 UnwindStackOfflineMap* map = new UnwindStackOfflineMap(pid);
214 if (!map->Build(maps, stack)) {
215 delete map;
216 return nullptr;
217 }
218 return map;
219}