blob: baf0ada281da38b4baf37634162b2fa6319c7fcb [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
18#include <assert.h>
19#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22#include <ucontext.h>
23
24#include <memory>
25#include <string>
26
27#if !defined(__ANDROID__)
28#include <cutils/threads.h>
29#endif
30
31#include <backtrace/Backtrace.h>
32#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
39#include "BacktraceLog.h"
40#include "UnwindStack.h"
41#include "UnwindStackMap.h"
42
43static std::string GetFunctionName(pid_t pid, BacktraceMap* back_map, uintptr_t pc,
44 uintptr_t* offset) {
45 *offset = 0;
46 unwindstack::Maps* maps = reinterpret_cast<UnwindStackMap*>(back_map)->stack_maps();
47
48 // Get the map for this
49 unwindstack::MapInfo* map_info = maps->Find(pc);
50 if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) {
51 return "";
52 }
53
54 unwindstack::Elf* elf = map_info->GetElf(pid, true);
55
56 std::string name;
57 uint64_t func_offset;
58 if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) {
59 return "";
60 }
61 *offset = func_offset;
62 return name;
63}
64
65static bool IsUnwindLibrary(const std::string& map_name) {
66 const std::string library(basename(map_name.c_str()));
67 return library == "libunwindstack.so" || library == "libbacktrace.so";
68}
69
70static bool Unwind(pid_t pid, unwindstack::Memory* memory, unwindstack::Regs* regs,
71 BacktraceMap* back_map, std::vector<backtrace_frame_data_t>* frames,
72 size_t num_ignore_frames) {
73 unwindstack::Maps* maps = reinterpret_cast<UnwindStackMap*>(back_map)->stack_maps();
74 bool adjust_rel_pc = false;
75 size_t num_frames = 0;
76 frames->clear();
77 while (num_frames < MAX_BACKTRACE_FRAMES) {
78 if (regs->pc() == 0) {
79 break;
80 }
81 unwindstack::MapInfo* map_info = maps->Find(regs->pc());
82 if (map_info == nullptr) {
83 break;
84 }
85
86 unwindstack::Elf* elf = map_info->GetElf(pid, true);
87 uint64_t rel_pc = regs->pc();
88 if (map_info != nullptr) {
89 rel_pc = elf->GetRelPc(regs->pc(), map_info);
90 }
91
92 bool skip_frame = num_frames == 0 && IsUnwindLibrary(map_info->name);
93 if (num_ignore_frames == 0 && !skip_frame) {
94 uint64_t adjusted_rel_pc = rel_pc;
95 if (map_info != nullptr && adjust_rel_pc) {
96 adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf);
97 }
98 frames->resize(num_frames + 1);
99 backtrace_frame_data_t* frame = &frames->at(num_frames);
100 frame->num = num_frames;
101 if (map_info != nullptr) {
102 // This will point to the adjusted absolute pc. regs->pc() is
103 // unaltered.
104 frame->pc = map_info->start + adjusted_rel_pc;
105 } else {
106 frame->pc = rel_pc;
107 }
108 frame->sp = regs->sp();
109 frame->rel_pc = adjusted_rel_pc;
110 frame->stack_size = 0;
111
112 frame->map.start = map_info->start;
113 frame->map.end = map_info->end;
114 frame->map.offset = map_info->offset;
115 frame->map.load_bias = elf->GetLoadBias();
116 frame->map.flags = map_info->flags;
117 frame->map.name = map_info->name;
118
119 uint64_t func_offset = 0;
120 if (!elf->GetFunctionName(adjusted_rel_pc, &frame->func_name, &func_offset)) {
121 frame->func_name = "";
122 }
123 frame->func_offset = func_offset;
124 if (num_frames > 0) {
125 // Set the stack size for the previous frame.
126 backtrace_frame_data_t* prev = &frames->at(num_frames - 1);
127 prev->stack_size = frame->sp - prev->sp;
128 }
129 num_frames++;
130 } else if (!skip_frame && num_ignore_frames > 0) {
131 num_ignore_frames--;
132 }
133 adjust_rel_pc = true;
134
135 // Do not unwind through a device map.
136 if (map_info->flags & PROT_DEVICE_MAP) {
137 break;
138 }
139 unwindstack::MapInfo* sp_info = maps->Find(regs->sp());
140 if (sp_info->flags & PROT_DEVICE_MAP) {
141 break;
142 }
143
144 if (!elf->Step(rel_pc + map_info->elf_offset, regs, memory)) {
145 break;
146 }
147 }
148
149 return true;
150}
151
152UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
153 : BacktraceCurrent(pid, tid, map), memory_(new unwindstack::MemoryLocal) {}
154
155std::string UnwindStackCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
156 return ::GetFunctionName(Pid(), GetMap(), pc, offset);
157}
158
159bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) {
160 std::unique_ptr<unwindstack::Regs> regs;
161 if (ucontext == nullptr) {
162 regs.reset(unwindstack::Regs::CreateFromLocal());
163 // Fill in the registers from this function. Do it here to avoid
164 // one extra function call appearing in the unwind.
165 unwindstack::RegsGetLocal(regs.get());
166 } else {
167 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::GetMachineType(), ucontext));
168 }
169
170 error_ = BACKTRACE_UNWIND_NO_ERROR;
171 return ::Unwind(getpid(), memory_.get(), regs.get(), GetMap(), &frames_, num_ignore_frames);
172}
173
174UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
175 : BacktracePtrace(pid, tid, map), memory_(new unwindstack::MemoryRemote(pid)) {}
176
177std::string UnwindStackPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
178 return ::GetFunctionName(Pid(), GetMap(), pc, offset);
179}
180
181bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) {
182 std::unique_ptr<unwindstack::Regs> regs;
183 if (context == nullptr) {
184 uint32_t machine_type;
185 regs.reset(unwindstack::Regs::RemoteGet(Tid(), &machine_type));
186 } else {
187 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::GetMachineType(), context));
188 }
189
190 error_ = BACKTRACE_UNWIND_NO_ERROR;
191 return ::Unwind(Pid(), memory_.get(), regs.get(), GetMap(), &frames_, num_ignore_frames);
192}
193
194Backtrace* Backtrace::CreateNew(pid_t pid, pid_t tid, BacktraceMap* map) {
195 if (pid == BACKTRACE_CURRENT_PROCESS) {
196 pid = getpid();
197 if (tid == BACKTRACE_CURRENT_THREAD) {
198 tid = gettid();
199 }
200 } else if (tid == BACKTRACE_CURRENT_THREAD) {
201 tid = pid;
202 }
203
204 if (map == nullptr) {
205// This would cause the wrong type of map object to be created, so disallow.
206#if defined(__ANDROID__)
207 __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__,
208 "Backtrace::CreateNew() must be called with a real map pointer.");
209#else
210 BACK_LOGE("Backtrace::CreateNew() must be called with a real map pointer.");
211 abort();
212#endif
213 }
214
215 if (pid == getpid()) {
216 return new UnwindStackCurrent(pid, tid, map);
217 } else {
218 return new UnwindStackPtrace(pid, tid, map);
219 }
220}