blob: 158467ec23c0bfb1d638f5ed25abe5703d18c43a [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#define _GNU_SOURCE 1
Christopher Ferris6f3981c2017-07-27 09:29:18 -070018#include <stdint.h>
19#include <stdlib.h>
20#include <string.h>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070021
22#include <memory>
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070023#include <set>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070024#include <string>
25
26#if !defined(__ANDROID__)
27#include <cutils/threads.h>
28#endif
29
30#include <backtrace/Backtrace.h>
Christopher Ferris04fdec02017-08-11 15:17:46 -070031#include <demangle.h>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070032#include <unwindstack/Elf.h>
33#include <unwindstack/MapInfo.h>
34#include <unwindstack/Maps.h>
35#include <unwindstack/Memory.h>
36#include <unwindstack/Regs.h>
37#include <unwindstack/RegsGetLocal.h>
38
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070039#include <unwindstack/Unwinder.h>
40
Christopher Ferris6f3981c2017-07-27 09:29:18 -070041#include "BacktraceLog.h"
Christopher Ferris0b06a592018-01-19 10:26:36 -080042#ifndef NO_LIBDEXFILE
43#include "UnwindDexFile.h"
44#endif
Christopher Ferris6f3981c2017-07-27 09:29:18 -070045#include "UnwindStack.h"
46#include "UnwindStackMap.h"
47
Christopher Ferris0b06a592018-01-19 10:26:36 -080048static void FillInDexFrame(UnwindStackMap* stack_map, uint64_t dex_pc,
49 backtrace_frame_data_t* frame) {
50 // The DEX PC points into the .dex section within an ELF file.
51 // However, this is a BBS section manually mmaped to a .vdex file,
52 // so we need to get the following map to find the ELF data.
53 unwindstack::Maps* maps = stack_map->stack_maps();
54 auto it = maps->begin();
55 uint64_t rel_dex_pc;
56 unwindstack::MapInfo* info;
57 for (; it != maps->end(); ++it) {
58 auto entry = *it;
59 if (dex_pc >= entry->start && dex_pc < entry->end) {
60 info = entry;
61 rel_dex_pc = dex_pc - entry->start;
62 frame->map.start = entry->start;
63 frame->map.end = entry->end;
64 frame->map.offset = entry->offset;
65 frame->map.load_bias = entry->load_bias;
66 frame->map.flags = entry->flags;
67 frame->map.name = entry->name;
68 frame->rel_pc = rel_dex_pc;
69 break;
70 }
71 }
72 if (it == maps->end() || ++it == maps->end()) {
73 return;
74 }
75
76 auto entry = *it;
77 auto process_memory = stack_map->process_memory();
78 unwindstack::Elf* elf = entry->GetElf(process_memory, true);
79 if (!elf->valid()) {
80 return;
81 }
82
83 // Adjust the relative dex by the offset.
84 rel_dex_pc += entry->elf_offset;
85
86 uint64_t dex_offset;
87 if (!elf->GetFunctionName(rel_dex_pc, &frame->func_name, &dex_offset)) {
88 return;
89 }
90 frame->func_offset = dex_offset;
91 if (frame->func_name != "$dexfile") {
92 return;
93 }
94
95#ifndef NO_LIBDEXFILE
96 UnwindDexFile* dex_file = stack_map->GetDexFile(dex_pc - dex_offset, info);
97 if (dex_file != nullptr) {
98 dex_file->GetMethodInformation(dex_offset, &frame->func_name, &frame->func_offset);
99 }
100#endif
101}
102
Josh Gao45c4a562017-09-06 14:35:35 -0700103bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700104 std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800105 std::vector<std::string>* skip_names, BacktraceUnwindError* error) {
Christopher Ferris5f118512017-09-01 11:17:16 -0700106 UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700107 auto process_memory = stack_map->process_memory();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700108 unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
109 regs, stack_map->process_memory());
Christopher Ferris2486d5a2018-01-22 17:37:59 -0800110 if (stack_map->GetJitDebug() != nullptr) {
111 unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch());
112 }
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700113 unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore());
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800114 if (error != nullptr) {
115 switch (unwinder.LastErrorCode()) {
116 case unwindstack::ERROR_NONE:
117 error->error_code = BACKTRACE_UNWIND_NO_ERROR;
118 break;
119
120 case unwindstack::ERROR_MEMORY_INVALID:
121 error->error_code = BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED;
122 error->error_info.addr = unwinder.LastErrorAddress();
123 break;
124
125 case unwindstack::ERROR_UNWIND_INFO:
126 error->error_code = BACKTRACE_UNWIND_ERROR_UNWIND_INFO;
127 break;
128
129 case unwindstack::ERROR_UNSUPPORTED:
130 error->error_code = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
131 break;
132
133 case unwindstack::ERROR_INVALID_MAP:
134 error->error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
135 break;
136
137 case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
138 error->error_code = BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT;
139 break;
140
141 case unwindstack::ERROR_REPEATED_FRAME:
142 error->error_code = BACKTRACE_UNWIND_ERROR_REPEATED_FRAME;
143 break;
144 }
145 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700146
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700147 if (num_ignore_frames >= unwinder.NumFrames()) {
148 frames->resize(0);
149 return true;
150 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700151
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700152 auto unwinder_frames = unwinder.frames();
Christopher Ferris0b06a592018-01-19 10:26:36 -0800153 // Get the real number of frames we'll need.
154 size_t total_frames = 0;
155 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++, total_frames++) {
156 if (unwinder_frames[i].dex_pc != 0) {
157 total_frames++;
158 }
159 }
160 frames->resize(total_frames);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700161 size_t cur_frame = 0;
David Srbecky645f8bb2018-01-25 12:21:59 +0000162 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) {
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700163 auto frame = &unwinder_frames[i];
David Srbecky645f8bb2018-01-25 12:21:59 +0000164
165 // Inject extra 'virtual' frame that represents the dex pc data.
166 // The dex pc is magic register defined in the Mterp interpreter,
167 // and thus it will be restored/observed in the frame after it.
168 // Adding the dex frame first here will create something like:
169 // #7 pc 006b1ba1 libartd.so ExecuteMterpImpl+14625
170 // #8 pc 0015fa20 core.vdex java.util.Arrays.binarySearch+8
171 // #9 pc 0039a1ef libartd.so art::interpreter::Execute+719
172 if (frame->dex_pc != 0) {
Christopher Ferris8fe58362018-01-26 14:26:13 -0800173 backtrace_frame_data_t* dex_frame = &frames->at(cur_frame);
174 dex_frame->num = cur_frame++;
David Srbecky645f8bb2018-01-25 12:21:59 +0000175 dex_frame->pc = frame->dex_pc;
176 dex_frame->rel_pc = frame->dex_pc;
177 dex_frame->sp = frame->sp;
178 dex_frame->stack_size = 0;
179 dex_frame->func_offset = 0;
180 FillInDexFrame(stack_map, frame->dex_pc, dex_frame);
181 }
182
Christopher Ferris8fe58362018-01-26 14:26:13 -0800183 backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700184
Christopher Ferris8fe58362018-01-26 14:26:13 -0800185 back_frame->num = cur_frame++;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700186
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700187 back_frame->rel_pc = frame->rel_pc;
188 back_frame->pc = frame->pc;
189 back_frame->sp = frame->sp;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700190
Christopher Ferris9a6b3e32017-10-18 09:08:51 -0700191 back_frame->func_name = demangle(frame->function_name.c_str());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700192 back_frame->func_offset = frame->function_offset;
193
194 back_frame->map.name = frame->map_name;
195 back_frame->map.start = frame->map_start;
196 back_frame->map.end = frame->map_end;
197 back_frame->map.offset = frame->map_offset;
198 back_frame->map.load_bias = frame->map_load_bias;
199 back_frame->map.flags = frame->map_flags;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700200 }
201
202 return true;
203}
204
205UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700206 : BacktraceCurrent(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700207
Christopher Ferris7937a362018-01-18 11:15:49 -0800208std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700209 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700210}
211
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800212bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700213 std::unique_ptr<unwindstack::Regs> regs;
214 if (ucontext == nullptr) {
215 regs.reset(unwindstack::Regs::CreateFromLocal());
216 // Fill in the registers from this function. Do it here to avoid
217 // one extra function call appearing in the unwind.
218 unwindstack::RegsGetLocal(regs.get());
219 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800220 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700221 }
222
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700223 std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800224 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700225}
226
227UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferrise3286732017-12-07 17:41:18 -0800228 : BacktracePtrace(pid, tid, map), memory_(pid) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700229
Christopher Ferris7937a362018-01-18 11:15:49 -0800230std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700231 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700232}
233
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800234bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700235 std::unique_ptr<unwindstack::Regs> regs;
236 if (context == nullptr) {
Josh Gao0953ecd2017-08-25 13:55:06 -0700237 regs.reset(unwindstack::Regs::RemoteGet(Tid()));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700238 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800239 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700240 }
241
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800242 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700243}
Christopher Ferrise3286732017-12-07 17:41:18 -0800244
Christopher Ferris7937a362018-01-18 11:15:49 -0800245size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
Christopher Ferrise3286732017-12-07 17:41:18 -0800246 return memory_.Read(addr, buffer, bytes);
247}
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800248
249UnwindStackOffline::UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map,
250 bool map_shared)
251 : Backtrace(pid, tid, map), arch_(arch) {
252 map_shared_ = map_shared;
253}
254
255bool UnwindStackOffline::Unwind(size_t num_ignore_frames, void* ucontext) {
256 if (ucontext == nullptr) {
257 return false;
258 }
259
260 unwindstack::ArchEnum arch;
261 switch (arch_) {
262 case ARCH_ARM:
263 arch = unwindstack::ARCH_ARM;
264 break;
265 case ARCH_ARM64:
266 arch = unwindstack::ARCH_ARM64;
267 break;
268 case ARCH_X86:
269 arch = unwindstack::ARCH_X86;
270 break;
271 case ARCH_X86_64:
272 arch = unwindstack::ARCH_X86_64;
273 break;
274 default:
275 return false;
276 }
277
278 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromUcontext(arch, ucontext));
279
280 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
281}
282
283std::string UnwindStackOffline::GetFunctionNameRaw(uint64_t, uint64_t*) {
284 return "";
285}
286
287size_t UnwindStackOffline::Read(uint64_t, uint8_t*, size_t) {
288 return 0;
289}
290
291bool UnwindStackOffline::ReadWord(uint64_t, word_t*) {
292 return false;
293}
294
295Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid,
296 const std::vector<backtrace_map_t>& maps,
297 const backtrace_stackinfo_t& stack) {
298 BacktraceMap* map = BacktraceMap::CreateOffline(pid, maps, stack);
299 if (map == nullptr) {
300 return nullptr;
301 }
302
303 return new UnwindStackOffline(arch, pid, tid, map, false);
304}
305
306Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map) {
307 if (map == nullptr) {
308 return nullptr;
309 }
310 return new UnwindStackOffline(arch, pid, tid, map, true);
311}
312
313void Backtrace::SetGlobalElfCache(bool enable) {
314 unwindstack::Elf::SetCachingEnabled(enable);
315}