blob: 83a5bb6668ed1f352388b82bff56ed32994e84cd [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);
Christopher Ferris3b4b0752017-08-09 11:16:03 -070087 uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info);
Christopher Ferris6f3981c2017-07-27 09:29:18 -070088
89 bool skip_frame = num_frames == 0 && IsUnwindLibrary(map_info->name);
90 if (num_ignore_frames == 0 && !skip_frame) {
91 uint64_t adjusted_rel_pc = rel_pc;
Christopher Ferris3b4b0752017-08-09 11:16:03 -070092 if (adjust_rel_pc) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070093 adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf);
94 }
95 frames->resize(num_frames + 1);
96 backtrace_frame_data_t* frame = &frames->at(num_frames);
97 frame->num = num_frames;
Christopher Ferris3b4b0752017-08-09 11:16:03 -070098 // This will point to the adjusted absolute pc. regs->pc() is
99 // unaltered.
100 frame->pc = map_info->start + adjusted_rel_pc;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700101 frame->sp = regs->sp();
102 frame->rel_pc = adjusted_rel_pc;
103 frame->stack_size = 0;
104
105 frame->map.start = map_info->start;
106 frame->map.end = map_info->end;
107 frame->map.offset = map_info->offset;
108 frame->map.load_bias = elf->GetLoadBias();
109 frame->map.flags = map_info->flags;
110 frame->map.name = map_info->name;
111
112 uint64_t func_offset = 0;
113 if (!elf->GetFunctionName(adjusted_rel_pc, &frame->func_name, &func_offset)) {
114 frame->func_name = "";
115 }
116 frame->func_offset = func_offset;
117 if (num_frames > 0) {
118 // Set the stack size for the previous frame.
119 backtrace_frame_data_t* prev = &frames->at(num_frames - 1);
120 prev->stack_size = frame->sp - prev->sp;
121 }
122 num_frames++;
123 } else if (!skip_frame && num_ignore_frames > 0) {
124 num_ignore_frames--;
125 }
126 adjust_rel_pc = true;
127
128 // Do not unwind through a device map.
129 if (map_info->flags & PROT_DEVICE_MAP) {
130 break;
131 }
132 unwindstack::MapInfo* sp_info = maps->Find(regs->sp());
133 if (sp_info->flags & PROT_DEVICE_MAP) {
134 break;
135 }
136
137 if (!elf->Step(rel_pc + map_info->elf_offset, regs, memory)) {
138 break;
139 }
140 }
141
142 return true;
143}
144
145UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
146 : BacktraceCurrent(pid, tid, map), memory_(new unwindstack::MemoryLocal) {}
147
148std::string UnwindStackCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
149 return ::GetFunctionName(Pid(), GetMap(), pc, offset);
150}
151
152bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) {
153 std::unique_ptr<unwindstack::Regs> regs;
154 if (ucontext == nullptr) {
155 regs.reset(unwindstack::Regs::CreateFromLocal());
156 // Fill in the registers from this function. Do it here to avoid
157 // one extra function call appearing in the unwind.
158 unwindstack::RegsGetLocal(regs.get());
159 } else {
160 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::GetMachineType(), ucontext));
161 }
162
163 error_ = BACKTRACE_UNWIND_NO_ERROR;
164 return ::Unwind(getpid(), memory_.get(), regs.get(), GetMap(), &frames_, num_ignore_frames);
165}
166
167UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
168 : BacktracePtrace(pid, tid, map), memory_(new unwindstack::MemoryRemote(pid)) {}
169
170std::string UnwindStackPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
171 return ::GetFunctionName(Pid(), GetMap(), pc, offset);
172}
173
174bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) {
175 std::unique_ptr<unwindstack::Regs> regs;
176 if (context == nullptr) {
177 uint32_t machine_type;
178 regs.reset(unwindstack::Regs::RemoteGet(Tid(), &machine_type));
179 } else {
180 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::GetMachineType(), context));
181 }
182
183 error_ = BACKTRACE_UNWIND_NO_ERROR;
184 return ::Unwind(Pid(), memory_.get(), regs.get(), GetMap(), &frames_, num_ignore_frames);
185}
186
187Backtrace* Backtrace::CreateNew(pid_t pid, pid_t tid, BacktraceMap* map) {
188 if (pid == BACKTRACE_CURRENT_PROCESS) {
189 pid = getpid();
190 if (tid == BACKTRACE_CURRENT_THREAD) {
191 tid = gettid();
192 }
193 } else if (tid == BACKTRACE_CURRENT_THREAD) {
194 tid = pid;
195 }
196
197 if (map == nullptr) {
198// This would cause the wrong type of map object to be created, so disallow.
199#if defined(__ANDROID__)
200 __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__,
201 "Backtrace::CreateNew() must be called with a real map pointer.");
202#else
203 BACK_LOGE("Backtrace::CreateNew() must be called with a real map pointer.");
204 abort();
205#endif
206 }
207
208 if (pid == getpid()) {
209 return new UnwindStackCurrent(pid, tid, map);
210 } else {
211 return new UnwindStackPtrace(pid, tid, map);
212 }
213}