blob: 25e5002ba5899bed3aab98cbed15a94aebfe9b66 [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
21#include <backtrace/BacktraceMap.h>
22#include <unwindstack/Elf.h>
23#include <unwindstack/MapInfo.h>
24#include <unwindstack/Maps.h>
25
26#include "UnwindStackMap.h"
27
28//-------------------------------------------------------------------------
29UnwindStackMap::UnwindStackMap(pid_t pid) : BacktraceMap(pid) {}
30
31bool UnwindStackMap::Build() {
32 if (pid_ == 0) {
33 pid_ = getpid();
34 stack_maps_.reset(new unwindstack::LocalMaps);
35 } else {
36 stack_maps_.reset(new unwindstack::RemoteMaps(pid_));
37 }
38
Christopher Ferris5f118512017-09-01 11:17:16 -070039 // Create the process memory object.
40 process_memory_ = unwindstack::Memory::CreateProcessMemory(pid_);
41
Christopher Ferris6f3981c2017-07-27 09:29:18 -070042 if (!stack_maps_->Parse()) {
43 return false;
44 }
45
46 // Iterate through the maps and fill in the backtrace_map_t structure.
47 for (auto& map_info : *stack_maps_) {
48 backtrace_map_t map;
49 map.start = map_info.start;
50 map.end = map_info.end;
51 map.offset = map_info.offset;
52 // Set to -1 so that it is demand loaded.
53 map.load_bias = static_cast<uintptr_t>(-1);
54 map.flags = map_info.flags;
55 map.name = map_info.name;
56
57 maps_.push_back(map);
58 }
59
60 return true;
61}
62
63void UnwindStackMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
64 BacktraceMap::FillIn(addr, map);
65 if (map->load_bias != static_cast<uintptr_t>(-1)) {
66 return;
67 }
68
69 // Fill in the load_bias.
70 unwindstack::MapInfo* map_info = stack_maps_->Find(addr);
71 if (map_info == nullptr) {
72 return;
73 }
Christopher Ferris5f118512017-09-01 11:17:16 -070074 unwindstack::Elf* elf = map_info->GetElf(process_memory_, true);
Christopher Ferris6f3981c2017-07-27 09:29:18 -070075 map->load_bias = elf->GetLoadBias();
76}
77
Josh Gao358de182017-09-06 22:16:09 -070078std::string UnwindStackMap::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
79 *offset = 0;
80 unwindstack::Maps* maps = stack_maps();
81
82 // Get the map for this
83 unwindstack::MapInfo* map_info = maps->Find(pc);
84 if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) {
85 return "";
86 }
87
88 unwindstack::Elf* elf = map_info->GetElf(process_memory(), true);
89
90 std::string name;
91 uint64_t func_offset;
92 if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) {
93 return "";
94 }
95 *offset = func_offset;
96 return name;
97}
98
Josh Gaoebea0ef2017-09-06 15:39:14 -070099std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() {
100 return process_memory_;
101}
102
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700103//-------------------------------------------------------------------------
104// BacktraceMap create function.
105//-------------------------------------------------------------------------
Christopher Ferris086baf92017-10-17 14:12:52 -0700106BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700107 BacktraceMap* map;
108
109 if (uncached) {
110 // Force use of the base class to parse the maps when this call is made.
111 map = new BacktraceMap(pid);
112 } else if (pid == getpid()) {
113 map = new UnwindStackMap(0);
114 } else {
115 map = new UnwindStackMap(pid);
116 }
117 if (!map->Build()) {
118 delete map;
119 return nullptr;
120 }
121 return map;
122}