• 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   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 
GetLoadBias(size_t index)102 uint64_t UnwindStackMap::GetLoadBias(size_t index) {
103   if (index >= stack_maps_->Total()) {
104     return 0;
105   }
106 
107   unwindstack::MapInfo* map_info = stack_maps_->Get(index);
108   if (map_info == nullptr) {
109     return 0;
110   }
111   return map_info->GetLoadBias(process_memory_);
112 }
113 
GetFunctionName(uint64_t pc,uint64_t * offset)114 std::string UnwindStackMap::GetFunctionName(uint64_t pc, uint64_t* offset) {
115   *offset = 0;
116   unwindstack::Maps* maps = stack_maps();
117 
118   // Get the map for this
119   unwindstack::MapInfo* map_info = maps->Find(pc);
120   if (map_info == nullptr || map_info->flags() & PROT_DEVICE_MAP) {
121     return "";
122   }
123 
124   if (arch_ == unwindstack::ARCH_UNKNOWN) {
125     if (pid_ == getpid()) {
126       arch_ = unwindstack::Regs::CurrentArch();
127     } else {
128       // Create a remote regs, to figure out the architecture.
129       std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::RemoteGet(pid_));
130       arch_ = regs->Arch();
131     }
132   }
133 
134   unwindstack::Elf* elf = map_info->GetElf(process_memory(), arch_);
135 
136   unwindstack::SharedString name;
137   uint64_t func_offset;
138   if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) {
139     return "";
140   }
141   *offset = func_offset;
142   return name;
143 }
144 
GetProcessMemory()145 std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() {
146   return process_memory_;
147 }
148 
149 //-------------------------------------------------------------------------
150 // BacktraceMap create function.
151 //-------------------------------------------------------------------------
Create(pid_t pid,bool uncached)152 BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
153   BacktraceMap* map;
154 
155   if (uncached) {
156     // Force use of the base class to parse the maps when this call is made.
157     map = new BacktraceMap(pid);
158   } else if (pid == getpid()) {
159     map = new UnwindStackMap(0);
160   } else {
161     map = new UnwindStackMap(pid);
162   }
163   if (!map->Build()) {
164     delete map;
165     return nullptr;
166   }
167   return map;
168 }
169