blob: c5dd45b861a3b9b89f2e8c3d10599b50a4c8e97b [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>
21#include <ucontext.h>
22
23#include <memory>
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070024#include <set>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070025#include <string>
26
27#if !defined(__ANDROID__)
28#include <cutils/threads.h>
29#endif
30
31#include <backtrace/Backtrace.h>
Christopher Ferris04fdec02017-08-11 15:17:46 -070032#include <demangle.h>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070033#include <unwindstack/Elf.h>
34#include <unwindstack/MapInfo.h>
35#include <unwindstack/Maps.h>
36#include <unwindstack/Memory.h>
37#include <unwindstack/Regs.h>
38#include <unwindstack/RegsGetLocal.h>
39
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070040#include <unwindstack/Unwinder.h>
41
Christopher Ferris6f3981c2017-07-27 09:29:18 -070042#include "BacktraceLog.h"
Christopher Ferris0b06a592018-01-19 10:26:36 -080043#ifndef NO_LIBDEXFILE
44#include "UnwindDexFile.h"
45#endif
Christopher Ferris6f3981c2017-07-27 09:29:18 -070046#include "UnwindStack.h"
47#include "UnwindStackMap.h"
48
Christopher Ferris0b06a592018-01-19 10:26:36 -080049static void FillInDexFrame(UnwindStackMap* stack_map, uint64_t dex_pc,
50 backtrace_frame_data_t* frame) {
51 // The DEX PC points into the .dex section within an ELF file.
52 // However, this is a BBS section manually mmaped to a .vdex file,
53 // so we need to get the following map to find the ELF data.
54 unwindstack::Maps* maps = stack_map->stack_maps();
55 auto it = maps->begin();
56 uint64_t rel_dex_pc;
57 unwindstack::MapInfo* info;
58 for (; it != maps->end(); ++it) {
59 auto entry = *it;
60 if (dex_pc >= entry->start && dex_pc < entry->end) {
61 info = entry;
62 rel_dex_pc = dex_pc - entry->start;
63 frame->map.start = entry->start;
64 frame->map.end = entry->end;
65 frame->map.offset = entry->offset;
66 frame->map.load_bias = entry->load_bias;
67 frame->map.flags = entry->flags;
68 frame->map.name = entry->name;
69 frame->rel_pc = rel_dex_pc;
70 break;
71 }
72 }
73 if (it == maps->end() || ++it == maps->end()) {
74 return;
75 }
76
77 auto entry = *it;
78 auto process_memory = stack_map->process_memory();
79 unwindstack::Elf* elf = entry->GetElf(process_memory, true);
80 if (!elf->valid()) {
81 return;
82 }
83
84 // Adjust the relative dex by the offset.
85 rel_dex_pc += entry->elf_offset;
86
87 uint64_t dex_offset;
88 if (!elf->GetFunctionName(rel_dex_pc, &frame->func_name, &dex_offset)) {
89 return;
90 }
91 frame->func_offset = dex_offset;
92 if (frame->func_name != "$dexfile") {
93 return;
94 }
95
96#ifndef NO_LIBDEXFILE
97 UnwindDexFile* dex_file = stack_map->GetDexFile(dex_pc - dex_offset, info);
98 if (dex_file != nullptr) {
99 dex_file->GetMethodInformation(dex_offset, &frame->func_name, &frame->func_offset);
100 }
101#endif
102}
103
Josh Gao45c4a562017-09-06 14:35:35 -0700104bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700105 std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
106 std::vector<std::string>* skip_names) {
Christopher Ferris5f118512017-09-01 11:17:16 -0700107 UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700108 auto process_memory = stack_map->process_memory();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700109 unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
110 regs, stack_map->process_memory());
Christopher Ferris2486d5a2018-01-22 17:37:59 -0800111 if (stack_map->GetJitDebug() != nullptr) {
112 unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch());
113 }
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700114 unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore());
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700115
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700116 if (num_ignore_frames >= unwinder.NumFrames()) {
117 frames->resize(0);
118 return true;
119 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700120
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700121 auto unwinder_frames = unwinder.frames();
Christopher Ferris0b06a592018-01-19 10:26:36 -0800122 // Get the real number of frames we'll need.
123 size_t total_frames = 0;
124 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++, total_frames++) {
125 if (unwinder_frames[i].dex_pc != 0) {
126 total_frames++;
127 }
128 }
129 frames->resize(total_frames);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700130 size_t cur_frame = 0;
David Srbecky645f8bb2018-01-25 12:21:59 +0000131 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) {
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700132 auto frame = &unwinder_frames[i];
David Srbecky645f8bb2018-01-25 12:21:59 +0000133
134 // Inject extra 'virtual' frame that represents the dex pc data.
135 // The dex pc is magic register defined in the Mterp interpreter,
136 // and thus it will be restored/observed in the frame after it.
137 // Adding the dex frame first here will create something like:
138 // #7 pc 006b1ba1 libartd.so ExecuteMterpImpl+14625
139 // #8 pc 0015fa20 core.vdex java.util.Arrays.binarySearch+8
140 // #9 pc 0039a1ef libartd.so art::interpreter::Execute+719
141 if (frame->dex_pc != 0) {
142 backtrace_frame_data_t* dex_frame = &frames->at(cur_frame++);
143 dex_frame->num = cur_frame;
144 dex_frame->pc = frame->dex_pc;
145 dex_frame->rel_pc = frame->dex_pc;
146 dex_frame->sp = frame->sp;
147 dex_frame->stack_size = 0;
148 dex_frame->func_offset = 0;
149 FillInDexFrame(stack_map, frame->dex_pc, dex_frame);
150 }
151
152 backtrace_frame_data_t* back_frame = &frames->at(cur_frame++);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700153
Christopher Ferris0b06a592018-01-19 10:26:36 -0800154 back_frame->num = cur_frame;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700155
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700156 back_frame->rel_pc = frame->rel_pc;
157 back_frame->pc = frame->pc;
158 back_frame->sp = frame->sp;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700159
Christopher Ferris9a6b3e32017-10-18 09:08:51 -0700160 back_frame->func_name = demangle(frame->function_name.c_str());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700161 back_frame->func_offset = frame->function_offset;
162
163 back_frame->map.name = frame->map_name;
164 back_frame->map.start = frame->map_start;
165 back_frame->map.end = frame->map_end;
166 back_frame->map.offset = frame->map_offset;
167 back_frame->map.load_bias = frame->map_load_bias;
168 back_frame->map.flags = frame->map_flags;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700169 }
170
171 return true;
172}
173
174UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700175 : BacktraceCurrent(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700176
Christopher Ferris7937a362018-01-18 11:15:49 -0800177std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700178 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700179}
180
181bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) {
182 std::unique_ptr<unwindstack::Regs> regs;
183 if (ucontext == nullptr) {
184 regs.reset(unwindstack::Regs::CreateFromLocal());
185 // Fill in the registers from this function. Do it here to avoid
186 // one extra function call appearing in the unwind.
187 unwindstack::RegsGetLocal(regs.get());
188 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800189 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700190 }
191
Yabin Cuif8808282017-12-12 18:04:10 -0800192 error_.error_code = BACKTRACE_UNWIND_NO_ERROR;
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700193 std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
194 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700195}
196
197UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferrise3286732017-12-07 17:41:18 -0800198 : BacktracePtrace(pid, tid, map), memory_(pid) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700199
Christopher Ferris7937a362018-01-18 11:15:49 -0800200std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700201 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700202}
203
204bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) {
205 std::unique_ptr<unwindstack::Regs> regs;
206 if (context == nullptr) {
Josh Gao0953ecd2017-08-25 13:55:06 -0700207 regs.reset(unwindstack::Regs::RemoteGet(Tid()));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700208 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800209 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700210 }
211
Yabin Cuif8808282017-12-12 18:04:10 -0800212 error_.error_code = BACKTRACE_UNWIND_NO_ERROR;
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700213 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700214}
Christopher Ferrise3286732017-12-07 17:41:18 -0800215
Christopher Ferris7937a362018-01-18 11:15:49 -0800216size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
Christopher Ferrise3286732017-12-07 17:41:18 -0800217 return memory_.Read(addr, buffer, bytes);
218}