blob: 41153ce19dd375d7b7c3d46c0f4d000b635d4242 [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>
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070025#include <set>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070026#include <string>
27
28#if !defined(__ANDROID__)
29#include <cutils/threads.h>
30#endif
31
32#include <backtrace/Backtrace.h>
Christopher Ferris04fdec02017-08-11 15:17:46 -070033#include <demangle.h>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070034#include <unwindstack/Elf.h>
35#include <unwindstack/MapInfo.h>
36#include <unwindstack/Maps.h>
37#include <unwindstack/Memory.h>
38#include <unwindstack/Regs.h>
39#include <unwindstack/RegsGetLocal.h>
40
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070041#include <unwindstack/Unwinder.h>
42
Christopher Ferris6f3981c2017-07-27 09:29:18 -070043#include "BacktraceLog.h"
44#include "UnwindStack.h"
45#include "UnwindStackMap.h"
46
Christopher Ferris5f118512017-09-01 11:17:16 -070047static std::string GetFunctionName(BacktraceMap* back_map, uintptr_t pc, uintptr_t* offset) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070048 *offset = 0;
49 unwindstack::Maps* maps = reinterpret_cast<UnwindStackMap*>(back_map)->stack_maps();
50
51 // Get the map for this
52 unwindstack::MapInfo* map_info = maps->Find(pc);
53 if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) {
54 return "";
55 }
56
Christopher Ferris5f118512017-09-01 11:17:16 -070057 UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
58 unwindstack::Elf* elf = map_info->GetElf(stack_map->process_memory(), true);
Christopher Ferris6f3981c2017-07-27 09:29:18 -070059
60 std::string name;
61 uint64_t func_offset;
62 if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) {
63 return "";
64 }
65 *offset = func_offset;
66 return name;
67}
68
Christopher Ferris5f118512017-09-01 11:17:16 -070069static bool Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
70 std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames) {
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070071 static std::set<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
Christopher Ferris5f118512017-09-01 11:17:16 -070072 UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070073 auto process_memory = stack_map->process_memory();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070074 unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
75 regs, stack_map->process_memory());
76 unwinder.Unwind(&skip_names);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070077
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070078 if (num_ignore_frames >= unwinder.NumFrames()) {
79 frames->resize(0);
80 return true;
81 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070082
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070083 frames->resize(unwinder.NumFrames() - num_ignore_frames);
84 auto unwinder_frames = unwinder.frames();
85 size_t cur_frame = 0;
86 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++, cur_frame++) {
87 auto frame = &unwinder_frames[i];
88 backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070089
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070090 back_frame->num = frame->num;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070091
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070092 back_frame->rel_pc = frame->rel_pc;
93 back_frame->pc = frame->pc;
94 back_frame->sp = frame->sp;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070095
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070096 back_frame->func_name = frame->function_name;
97 back_frame->func_offset = frame->function_offset;
98
99 back_frame->map.name = frame->map_name;
100 back_frame->map.start = frame->map_start;
101 back_frame->map.end = frame->map_end;
102 back_frame->map.offset = frame->map_offset;
103 back_frame->map.load_bias = frame->map_load_bias;
104 back_frame->map.flags = frame->map_flags;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700105 }
106
107 return true;
108}
109
110UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700111 : BacktraceCurrent(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700112
113std::string UnwindStackCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
Christopher Ferris5f118512017-09-01 11:17:16 -0700114 return ::GetFunctionName(GetMap(), pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700115}
116
117bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) {
118 std::unique_ptr<unwindstack::Regs> regs;
119 if (ucontext == nullptr) {
120 regs.reset(unwindstack::Regs::CreateFromLocal());
121 // Fill in the registers from this function. Do it here to avoid
122 // one extra function call appearing in the unwind.
123 unwindstack::RegsGetLocal(regs.get());
124 } else {
Josh Gao0953ecd2017-08-25 13:55:06 -0700125 regs.reset(
126 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentMachineType(), ucontext));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700127 }
128
129 error_ = BACKTRACE_UNWIND_NO_ERROR;
Christopher Ferris5f118512017-09-01 11:17:16 -0700130 return ::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700131}
132
133UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700134 : BacktracePtrace(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700135
136std::string UnwindStackPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
Christopher Ferris5f118512017-09-01 11:17:16 -0700137 return ::GetFunctionName(GetMap(), pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700138}
139
140bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) {
141 std::unique_ptr<unwindstack::Regs> regs;
142 if (context == nullptr) {
Josh Gao0953ecd2017-08-25 13:55:06 -0700143 regs.reset(unwindstack::Regs::RemoteGet(Tid()));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700144 } else {
Josh Gao0953ecd2017-08-25 13:55:06 -0700145 regs.reset(
146 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentMachineType(), context));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700147 }
148
149 error_ = BACKTRACE_UNWIND_NO_ERROR;
Christopher Ferris5f118512017-09-01 11:17:16 -0700150 return ::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700151}
152
153Backtrace* Backtrace::CreateNew(pid_t pid, pid_t tid, BacktraceMap* map) {
154 if (pid == BACKTRACE_CURRENT_PROCESS) {
155 pid = getpid();
156 if (tid == BACKTRACE_CURRENT_THREAD) {
157 tid = gettid();
158 }
159 } else if (tid == BACKTRACE_CURRENT_THREAD) {
160 tid = pid;
161 }
162
163 if (map == nullptr) {
164// This would cause the wrong type of map object to be created, so disallow.
165#if defined(__ANDROID__)
166 __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__,
167 "Backtrace::CreateNew() must be called with a real map pointer.");
168#else
169 BACK_LOGE("Backtrace::CreateNew() must be called with a real map pointer.");
170 abort();
171#endif
172 }
173
174 if (pid == getpid()) {
175 return new UnwindStackCurrent(pid, tid, map);
176 } else {
177 return new UnwindStackPtrace(pid, tid, map);
178 }
179}