blob: 11ff84a8b9448f70b90719857f14fd30d38ef4d3 [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 Ferris6f3981c2017-07-27 09:29:18 -0700150//-------------------------------------------------------------------------
151// BacktraceMap create function.
152//-------------------------------------------------------------------------
Christopher Ferris086baf92017-10-17 14:12:52 -0700153BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700154 BacktraceMap* map;
155
156 if (uncached) {
157 // Force use of the base class to parse the maps when this call is made.
158 map = new BacktraceMap(pid);
159 } else if (pid == getpid()) {
160 map = new UnwindStackMap(0);
161 } else {
162 map = new UnwindStackMap(pid);
163 }
164 if (!map->Build()) {
165 delete map;
166 return nullptr;
167 }
168 return map;
169}