blob: 2a555afc4ef8c4dbb5943a29a4e3da2984e9f59d [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
Josh Gao45c4a562017-09-06 14:35:35 -070046bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
Christopher Ferrisc56a4992017-11-02 16:19:56 -070047 std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
48 std::vector<std::string>* skip_names) {
Christopher Ferris5f118512017-09-01 11:17:16 -070049 UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070050 auto process_memory = stack_map->process_memory();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070051 unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
52 regs, stack_map->process_memory());
Christopher Ferrisc56a4992017-11-02 16:19:56 -070053 unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore());
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070054
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070055 if (num_ignore_frames >= unwinder.NumFrames()) {
56 frames->resize(0);
57 return true;
58 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070059
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070060 frames->resize(unwinder.NumFrames() - num_ignore_frames);
61 auto unwinder_frames = unwinder.frames();
62 size_t cur_frame = 0;
63 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++, cur_frame++) {
64 auto frame = &unwinder_frames[i];
65 backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070066
Josh Gaocd546c12017-10-25 15:21:45 -070067 back_frame->num = frame->num - num_ignore_frames;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070068
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070069 back_frame->rel_pc = frame->rel_pc;
70 back_frame->pc = frame->pc;
71 back_frame->sp = frame->sp;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070072
Christopher Ferris9a6b3e32017-10-18 09:08:51 -070073 back_frame->func_name = demangle(frame->function_name.c_str());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070074 back_frame->func_offset = frame->function_offset;
75
76 back_frame->map.name = frame->map_name;
77 back_frame->map.start = frame->map_start;
78 back_frame->map.end = frame->map_end;
79 back_frame->map.offset = frame->map_offset;
80 back_frame->map.load_bias = frame->map_load_bias;
81 back_frame->map.flags = frame->map_flags;
Christopher Ferris6f3981c2017-07-27 09:29:18 -070082 }
83
84 return true;
85}
86
87UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -070088 : BacktraceCurrent(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -070089
90std::string UnwindStackCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -070091 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -070092}
93
94bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) {
95 std::unique_ptr<unwindstack::Regs> regs;
96 if (ucontext == nullptr) {
97 regs.reset(unwindstack::Regs::CreateFromLocal());
98 // Fill in the registers from this function. Do it here to avoid
99 // one extra function call appearing in the unwind.
100 unwindstack::RegsGetLocal(regs.get());
101 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800102 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700103 }
104
Yabin Cuif8808282017-12-12 18:04:10 -0800105 error_.error_code = BACKTRACE_UNWIND_NO_ERROR;
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700106 std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
107 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700108}
109
110UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferrise3286732017-12-07 17:41:18 -0800111 : BacktracePtrace(pid, tid, map), memory_(pid) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700112
113std::string UnwindStackPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700114 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700115}
116
117bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) {
118 std::unique_ptr<unwindstack::Regs> regs;
119 if (context == nullptr) {
Josh Gao0953ecd2017-08-25 13:55:06 -0700120 regs.reset(unwindstack::Regs::RemoteGet(Tid()));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700121 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800122 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700123 }
124
Yabin Cuif8808282017-12-12 18:04:10 -0800125 error_.error_code = BACKTRACE_UNWIND_NO_ERROR;
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700126 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700127}
Christopher Ferrise3286732017-12-07 17:41:18 -0800128
129size_t UnwindStackPtrace::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
130 return memory_.Read(addr, buffer, bytes);
131}