• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <string>
22 #include <vector>
23 
24 #include <backtrace/BacktraceMap.h>
25 #include <unwindstack/Elf.h>
26 #include <unwindstack/MapInfo.h>
27 #include <unwindstack/Maps.h>
28 #include <unwindstack/Regs.h>
29 
30 #include "UnwindStackMap.h"
31 
32 //-------------------------------------------------------------------------
UnwindStackMap(pid_t pid)33 UnwindStackMap::UnwindStackMap(pid_t pid) : BacktraceMap(pid) {}
34 
Build()35 bool 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 
43   // Create the process memory object.
44   process_memory_ = unwindstack::Memory::CreateProcessMemory(pid_);
45 
46   if (arch_ == unwindstack::ARCH_UNKNOWN) {
47     if (pid_ == getpid()) {
48       arch_ = unwindstack::Regs::CurrentArch();
49     } else {
50       // Create a remote regs, to figure out the architecture.
51       std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::RemoteGet(pid_));
52       if (regs.get() != nullptr) {
53         arch_ = regs->Arch();
54       }
55     }
56   }
57 
58   // Create a JitDebug object for getting jit unwind information.
59   if (arch_ != unwindstack::ARCH_UNKNOWN) {
60     std::vector<std::string> search_libs_{"libart.so", "libartd.so"};
61     jit_debug_ = CreateJitDebug(arch_, process_memory_, search_libs_);
62 #if !defined(NO_LIBDEXFILE_SUPPORT)
63     dex_files_ = CreateDexFiles(arch_, process_memory_, search_libs_);
64 #endif
65   }
66 
67   if (!stack_maps_->Parse()) {
68     return false;
69   }
70 
71   // Iterate through the maps and fill in the backtrace_map_t structure.
72   for (const auto& map_info : *stack_maps_) {
73     backtrace_map_t map;
74     map.start = map_info->start();
75     map.end = map_info->end();
76     map.offset = map_info->offset();
77     // Set to -1 so that it is demand loaded.
78     map.load_bias = static_cast<uint64_t>(-1);
79     map.flags = map_info->flags();
80     map.name = map_info->name();
81 
82     maps_.push_back(map);
83   }
84 
85   return true;
86 }
87 
FillIn(uint64_t addr,backtrace_map_t * map)88 void UnwindStackMap::FillIn(uint64_t addr, backtrace_map_t* map) {
89   BacktraceMap::FillIn(addr, map);
90   if (map->load_bias != static_cast<uint64_t>(-1)) {
91     return;
92   }
93 
94   // Fill in the load_bias.
95   std::shared_ptr<unwindstack::MapInfo> map_info = stack_maps_->Find(addr);
96   if (map_info == nullptr) {
97     return;
98   }
99   map->load_bias = map_info->GetLoadBias(process_memory_);
100 }
101 
GetBuildId(uint64_t addr)102 std::string UnwindStackMap::GetBuildId(uint64_t addr) {
103   auto map_info = stack_maps_->Find(addr);
104   return map_info == nullptr ? std::string() : map_info->GetPrintableBuildID();
105 }
106 
GetLoadBias(size_t index)107 uint64_t UnwindStackMap::GetLoadBias(size_t index) {
108   if (index >= stack_maps_->Total()) {
109     return 0;
110   }
111 
112   std::shared_ptr<unwindstack::MapInfo> map_info = stack_maps_->Get(index);
113   if (map_info == nullptr) {
114     return 0;
115   }
116   return map_info->GetLoadBias(process_memory_);
117 }
118 
GetFunctionName(uint64_t pc,uint64_t * offset)119 std::string UnwindStackMap::GetFunctionName(uint64_t pc, uint64_t* offset) {
120   *offset = 0;
121   unwindstack::Maps* maps = stack_maps();
122 
123   // Get the map for this
124   auto map_info = maps->Find(pc);
125   if (map_info == nullptr || map_info->flags() & PROT_DEVICE_MAP) {
126     return "";
127   }
128 
129   if (arch_ == unwindstack::ARCH_UNKNOWN) {
130     if (pid_ == getpid()) {
131       arch_ = unwindstack::Regs::CurrentArch();
132     } else {
133       // Create a remote regs, to figure out the architecture.
134       std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::RemoteGet(pid_));
135       arch_ = regs->Arch();
136     }
137   }
138 
139   unwindstack::Elf* elf = map_info->GetElf(process_memory(), arch_);
140 
141   unwindstack::SharedString name;
142   uint64_t func_offset;
143   if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info.get()), &name, &func_offset)) {
144     return "";
145   }
146   *offset = func_offset;
147   return name;
148 }
149 
GetProcessMemory()150 std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() {
151   return process_memory_;
152 }
153 
154 //-------------------------------------------------------------------------
155 // BacktraceMap create function.
156 //-------------------------------------------------------------------------
Create(pid_t pid,bool uncached)157 BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
158   BacktraceMap* map;
159 
160   if (uncached) {
161     // Force use of the base class to parse the maps when this call is made.
162     map = new BacktraceMap(pid);
163   } else if (pid == getpid()) {
164     map = new UnwindStackMap(0);
165   } else {
166     map = new UnwindStackMap(pid);
167   }
168   if (!map->Build()) {
169     delete map;
170     return nullptr;
171   }
172   return map;
173 }
174