blob: 60c7952a96cc9be4b2d66927b395268ae5aadf24 [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
29#include "UnwindStackMap.h"
30
31//-------------------------------------------------------------------------
32UnwindStackMap::UnwindStackMap(pid_t pid) : BacktraceMap(pid) {}
33
34bool UnwindStackMap::Build() {
35 if (pid_ == 0) {
36 pid_ = getpid();
37 stack_maps_.reset(new unwindstack::LocalMaps);
38 } else {
39 stack_maps_.reset(new unwindstack::RemoteMaps(pid_));
40 }
41
Christopher Ferris5f118512017-09-01 11:17:16 -070042 // Create the process memory object.
43 process_memory_ = unwindstack::Memory::CreateProcessMemory(pid_);
44
Christopher Ferris150db122017-12-20 18:49:01 -080045 // Create a JitDebug object for getting jit unwind information.
46 std::vector<std::string> search_libs_{"libart.so", "libartd.so"};
47 jit_debug_.reset(new unwindstack::JitDebug(process_memory_, search_libs_));
48
Christopher Ferris6f3981c2017-07-27 09:29:18 -070049 if (!stack_maps_->Parse()) {
50 return false;
51 }
52
53 // Iterate through the maps and fill in the backtrace_map_t structure.
Christopher Ferrisbe788d82017-11-27 14:50:38 -080054 for (auto* map_info : *stack_maps_) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070055 backtrace_map_t map;
Christopher Ferrisbe788d82017-11-27 14:50:38 -080056 map.start = map_info->start;
57 map.end = map_info->end;
58 map.offset = map_info->offset;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070059 // Set to -1 so that it is demand loaded.
Christopher Ferris7937a362018-01-18 11:15:49 -080060 map.load_bias = static_cast<uint64_t>(-1);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080061 map.flags = map_info->flags;
62 map.name = map_info->name;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070063
64 maps_.push_back(map);
65 }
66
67 return true;
68}
69
Christopher Ferris7937a362018-01-18 11:15:49 -080070void UnwindStackMap::FillIn(uint64_t addr, backtrace_map_t* map) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070071 BacktraceMap::FillIn(addr, map);
Christopher Ferris7937a362018-01-18 11:15:49 -080072 if (map->load_bias != static_cast<uint64_t>(-1)) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070073 return;
74 }
75
76 // Fill in the load_bias.
77 unwindstack::MapInfo* map_info = stack_maps_->Find(addr);
78 if (map_info == nullptr) {
79 return;
80 }
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080081 map->load_bias = map_info->GetLoadBias(process_memory_);
82}
83
84uint64_t UnwindStackMap::GetLoadBias(size_t index) {
85 if (index >= stack_maps_->Total()) {
86 return 0;
87 }
88
89 unwindstack::MapInfo* map_info = stack_maps_->Get(index);
90 if (map_info == nullptr) {
91 return 0;
92 }
93 return map_info->GetLoadBias(process_memory_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -070094}
95
Christopher Ferris7937a362018-01-18 11:15:49 -080096std::string UnwindStackMap::GetFunctionName(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -070097 *offset = 0;
98 unwindstack::Maps* maps = stack_maps();
99
100 // Get the map for this
101 unwindstack::MapInfo* map_info = maps->Find(pc);
102 if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) {
103 return "";
104 }
105
106 unwindstack::Elf* elf = map_info->GetElf(process_memory(), true);
107
108 std::string name;
109 uint64_t func_offset;
110 if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) {
111 return "";
112 }
113 *offset = func_offset;
114 return name;
115}
116
Josh Gaoebea0ef2017-09-06 15:39:14 -0700117std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() {
118 return process_memory_;
119}
120
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700121//-------------------------------------------------------------------------
122// BacktraceMap create function.
123//-------------------------------------------------------------------------
Christopher Ferris086baf92017-10-17 14:12:52 -0700124BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700125 BacktraceMap* map;
126
127 if (uncached) {
128 // Force use of the base class to parse the maps when this call is made.
129 map = new BacktraceMap(pid);
130 } else if (pid == getpid()) {
131 map = new UnwindStackMap(0);
132 } else {
133 map = new UnwindStackMap(pid);
134 }
135 if (!map->Build()) {
136 delete map;
137 return nullptr;
138 }
139 return map;
140}