blob: 73bfdec1e03ff5220454e1fd58e18d89b28f0888 [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"
43#include "UnwindStack.h"
44#include "UnwindStackMap.h"
45
Christopher Ferris5f118512017-09-01 11:17:16 -070046static std::string GetFunctionName(BacktraceMap* back_map, uintptr_t pc, uintptr_t* offset) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -070047 *offset = 0;
48 unwindstack::Maps* maps = reinterpret_cast<UnwindStackMap*>(back_map)->stack_maps();
49
50 // Get the map for this
51 unwindstack::MapInfo* map_info = maps->Find(pc);
52 if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) {
53 return "";
54 }
55
Christopher Ferris5f118512017-09-01 11:17:16 -070056 UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
57 unwindstack::Elf* elf = map_info->GetElf(stack_map->process_memory(), true);
Christopher Ferris6f3981c2017-07-27 09:29:18 -070058
59 std::string name;
60 uint64_t func_offset;
61 if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) {
62 return "";
63 }
64 *offset = func_offset;
65 return name;
66}
67
Christopher Ferris5f118512017-09-01 11:17:16 -070068static bool Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
69 std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames) {
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070070 static std::set<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
Christopher Ferris5f118512017-09-01 11:17:16 -070071 UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070072 auto process_memory = stack_map->process_memory();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070073 unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
74 regs, stack_map->process_memory());
75 unwinder.Unwind(&skip_names);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070076
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070077 if (num_ignore_frames >= unwinder.NumFrames()) {
78 frames->resize(0);
79 return true;
80 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070081
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070082 frames->resize(unwinder.NumFrames() - num_ignore_frames);
83 auto unwinder_frames = unwinder.frames();
84 size_t cur_frame = 0;
85 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++, cur_frame++) {
86 auto frame = &unwinder_frames[i];
87 backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070088
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070089 back_frame->num = frame->num;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070090
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070091 back_frame->rel_pc = frame->rel_pc;
92 back_frame->pc = frame->pc;
93 back_frame->sp = frame->sp;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070094
Christopher Ferris9a6b3e32017-10-18 09:08:51 -070095 back_frame->func_name = demangle(frame->function_name.c_str());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070096 back_frame->func_offset = frame->function_offset;
97
98 back_frame->map.name = frame->map_name;
99 back_frame->map.start = frame->map_start;
100 back_frame->map.end = frame->map_end;
101 back_frame->map.offset = frame->map_offset;
102 back_frame->map.load_bias = frame->map_load_bias;
103 back_frame->map.flags = frame->map_flags;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700104 }
105
106 return true;
107}
108
109UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700110 : BacktraceCurrent(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700111
112std::string UnwindStackCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
Christopher Ferris5f118512017-09-01 11:17:16 -0700113 return ::GetFunctionName(GetMap(), pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700114}
115
116bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) {
117 std::unique_ptr<unwindstack::Regs> regs;
118 if (ucontext == nullptr) {
119 regs.reset(unwindstack::Regs::CreateFromLocal());
120 // Fill in the registers from this function. Do it here to avoid
121 // one extra function call appearing in the unwind.
122 unwindstack::RegsGetLocal(regs.get());
123 } else {
Josh Gao0953ecd2017-08-25 13:55:06 -0700124 regs.reset(
125 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentMachineType(), ucontext));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700126 }
127
128 error_ = BACKTRACE_UNWIND_NO_ERROR;
Christopher Ferris5f118512017-09-01 11:17:16 -0700129 return ::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700130}
131
132UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700133 : BacktracePtrace(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700134
135std::string UnwindStackPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
Christopher Ferris5f118512017-09-01 11:17:16 -0700136 return ::GetFunctionName(GetMap(), pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700137}
138
139bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) {
140 std::unique_ptr<unwindstack::Regs> regs;
141 if (context == nullptr) {
Josh Gao0953ecd2017-08-25 13:55:06 -0700142 regs.reset(unwindstack::Regs::RemoteGet(Tid()));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700143 } else {
Josh Gao0953ecd2017-08-25 13:55:06 -0700144 regs.reset(
145 unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentMachineType(), context));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700146 }
147
148 error_ = BACKTRACE_UNWIND_NO_ERROR;
Christopher Ferris5f118512017-09-01 11:17:16 -0700149 return ::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700150}