blob: aa0b17c991d0ffe45f8469d454bb775702e09366 [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
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000046 // Create a JitDebug object for getting jit unwind information.
47 std::vector<std::string> search_libs_{"libart.so", "libartd.so"};
48 jit_debug_.reset(new unwindstack::JitDebug(process_memory_, search_libs_));
49#if !defined(NO_LIBDEXFILE_SUPPORT)
50 dex_files_.reset(new unwindstack::DexFiles(process_memory_, search_libs_));
51#endif
52
Christopher Ferris6f3981c2017-07-27 09:29:18 -070053 if (!stack_maps_->Parse()) {
54 return false;
55 }
56
57 // Iterate through the maps and fill in the backtrace_map_t structure.
Florian Mayer3d67d342019-02-27 18:00:37 +000058 for (const auto& map_info : *stack_maps_) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070059 backtrace_map_t map;
Christopher Ferrisbe788d82017-11-27 14:50:38 -080060 map.start = map_info->start;
61 map.end = map_info->end;
62 map.offset = map_info->offset;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070063 // Set to -1 so that it is demand loaded.
Christopher Ferris7937a362018-01-18 11:15:49 -080064 map.load_bias = static_cast<uint64_t>(-1);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080065 map.flags = map_info->flags;
66 map.name = map_info->name;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070067
68 maps_.push_back(map);
69 }
70
71 return true;
72}
73
Christopher Ferris7937a362018-01-18 11:15:49 -080074void UnwindStackMap::FillIn(uint64_t addr, backtrace_map_t* map) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070075 BacktraceMap::FillIn(addr, map);
Christopher Ferris7937a362018-01-18 11:15:49 -080076 if (map->load_bias != static_cast<uint64_t>(-1)) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070077 return;
78 }
79
80 // Fill in the load_bias.
81 unwindstack::MapInfo* map_info = stack_maps_->Find(addr);
82 if (map_info == nullptr) {
83 return;
84 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080085 map->load_bias = map_info->GetLoadBias(process_memory_);
86}
87
88uint64_t UnwindStackMap::GetLoadBias(size_t index) {
89 if (index >= stack_maps_->Total()) {
90 return 0;
91 }
92
93 unwindstack::MapInfo* map_info = stack_maps_->Get(index);
94 if (map_info == nullptr) {
95 return 0;
96 }
97 return map_info->GetLoadBias(process_memory_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -070098}
99
Christopher Ferris7937a362018-01-18 11:15:49 -0800100std::string UnwindStackMap::GetFunctionName(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700101 *offset = 0;
102 unwindstack::Maps* maps = stack_maps();
103
104 // Get the map for this
105 unwindstack::MapInfo* map_info = maps->Find(pc);
106 if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) {
107 return "";
108 }
109
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700110 if (arch_ == unwindstack::ARCH_UNKNOWN) {
111 if (pid_ == getpid()) {
112 arch_ = unwindstack::Regs::CurrentArch();
113 } else {
114 // Create a remote regs, to figure out the architecture.
115 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::RemoteGet(pid_));
116 arch_ = regs->Arch();
117 }
118 }
119
120 unwindstack::Elf* elf = map_info->GetElf(process_memory(), arch_);
Josh Gao358de182017-09-06 22:16:09 -0700121
122 std::string name;
123 uint64_t func_offset;
124 if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) {
125 return "";
126 }
127 *offset = func_offset;
128 return name;
129}
130
Josh Gaoebea0ef2017-09-06 15:39:14 -0700131std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() {
132 return process_memory_;
133}
134
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700135//-------------------------------------------------------------------------
136// BacktraceMap create function.
137//-------------------------------------------------------------------------
Christopher Ferris086baf92017-10-17 14:12:52 -0700138BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700139 BacktraceMap* map;
140
141 if (uncached) {
142 // Force use of the base class to parse the maps when this call is made.
143 map = new BacktraceMap(pid);
144 } else if (pid == getpid()) {
145 map = new UnwindStackMap(0);
146 } else {
147 map = new UnwindStackMap(pid);
148 }
149 if (!map->Build()) {
150 delete map;
151 return nullptr;
152 }
153 return map;
154}