blob: 63b8c7c0343df76c1e3335eba7997ecabde437f6 [file] [log] [blame]
Christopher Ferris17e91d42013-10-21 13:30:52 -07001/*
2 * Copyright (C) 2013 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 LOG_TAG "libbacktrace"
18
19#include <backtrace/backtrace.h>
20
21#include <string.h>
22
23#include <backtrace-arch.h>
24#include <cutils/log.h>
25#include <corkscrew/backtrace.h>
26
27#ifndef __USE_GNU
28#define __USE_GNU
29#endif
30#include <dlfcn.h>
31
32#include "Corkscrew.h"
33
34//-------------------------------------------------------------------------
35// CorkscrewCommon functions.
36//-------------------------------------------------------------------------
37bool CorkscrewCommon::GenerateFrameData(
38 backtrace_frame_t* cork_frames, ssize_t num_frames) {
39 if (num_frames < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070040 BACK_LOGW("libcorkscrew unwind failed.");
Christopher Ferris17e91d42013-10-21 13:30:52 -070041 return false;
42 }
43
44 backtrace_t* data = GetBacktraceData();
45 data->num_frames = num_frames;
46 for (size_t i = 0; i < data->num_frames; i++) {
47 backtrace_frame_data_t* frame = &data->frames[i];
Christopher Ferris20303f82014-01-10 16:33:16 -080048 frame->num = i;
Christopher Ferris17e91d42013-10-21 13:30:52 -070049 frame->pc = cork_frames[i].absolute_pc;
50 frame->sp = cork_frames[i].stack_top;
51 frame->stack_size = cork_frames[i].stack_size;
52 frame->map_name = NULL;
53 frame->map_offset = 0;
54 frame->func_name = NULL;
55 frame->func_offset = 0;
56
57 uintptr_t map_start;
58 frame->map_name = backtrace_obj_->GetMapName(frame->pc, &map_start);
59 if (frame->map_name) {
60 frame->map_offset = frame->pc - map_start;
61 }
62
63 std::string func_name = backtrace_obj_->GetFunctionName(frame->pc, &frame->func_offset);
64 if (!func_name.empty()) {
65 frame->func_name = strdup(func_name.c_str());
66 }
67 }
68 return true;
69}
70
71//-------------------------------------------------------------------------
72// CorkscrewCurrent functions.
73//-------------------------------------------------------------------------
74CorkscrewCurrent::CorkscrewCurrent() {
75}
76
77CorkscrewCurrent::~CorkscrewCurrent() {
78}
79
80bool CorkscrewCurrent::Unwind(size_t num_ignore_frames) {
81 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
82 ssize_t num_frames = unwind_backtrace(frames, num_ignore_frames, MAX_BACKTRACE_FRAMES);
83
84 return GenerateFrameData(frames, num_frames);
85}
86
87std::string CorkscrewCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
88 *offset = 0;
89
Christopher Ferris17e91d42013-10-21 13:30:52 -070090 Dl_info info;
91 const backtrace_map_info_t* map_info = backtrace_obj_->FindMapInfo(pc);
Christopher Ferris923b5362013-11-04 14:38:07 -080092 if (map_info) {
93 if (dladdr((const void*)pc, &info)) {
94 if (info.dli_sname) {
95 *offset = pc - map_info->start - (uintptr_t)info.dli_saddr + (uintptr_t)info.dli_fbase;
96 return info.dli_sname;
97 }
98 } else {
99 // dladdr(3) didn't find a symbol; maybe it's static? Look in the ELF file...
100 symbol_table_t* symbol_table = load_symbol_table(map_info->name);
101 if (symbol_table) {
102 // First check if we can find the symbol using a relative pc.
103 std::string name;
104 const symbol_t* elf_symbol = find_symbol(symbol_table, pc - map_info->start);
105 if (elf_symbol) {
106 name = elf_symbol->name;
107 *offset = pc - map_info->start - elf_symbol->start;
108 } else if ((elf_symbol = find_symbol(symbol_table, pc)) != NULL) {
109 // Found the symbol using the absolute pc.
110 name = elf_symbol->name;
111 *offset = pc - elf_symbol->start;
112 }
113 free_symbol_table(symbol_table);
114 return name;
115 }
116 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700117 }
118 return "";
119}
120
121//-------------------------------------------------------------------------
122// CorkscrewThread functions.
123//-------------------------------------------------------------------------
124CorkscrewThread::CorkscrewThread() {
125}
126
127CorkscrewThread::~CorkscrewThread() {
128 if (corkscrew_map_info_) {
129 free_map_info_list(corkscrew_map_info_);
130 corkscrew_map_info_ = NULL;
131 }
132}
133
134bool CorkscrewThread::Init() {
135 corkscrew_map_info_ = load_map_info_list(backtrace_obj_->Pid());
136 return corkscrew_map_info_ != NULL;
137}
138
139void CorkscrewThread::ThreadUnwind(
140 siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) {
141 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
142 ssize_t num_frames = unwind_backtrace_signal_arch(
143 siginfo, sigcontext, corkscrew_map_info_, frames, num_ignore_frames,
144 MAX_BACKTRACE_FRAMES);
145 if (num_frames > 0) {
146 backtrace_t* data = GetBacktraceData();
147 data->num_frames = num_frames;
148 for (size_t i = 0; i < data->num_frames; i++) {
149 backtrace_frame_data_t* frame = &data->frames[i];
Christopher Ferris20303f82014-01-10 16:33:16 -0800150 frame->num = i;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700151 frame->pc = frames[i].absolute_pc;
152 frame->sp = frames[i].stack_top;
153 frame->stack_size = frames[i].stack_size;
154
155 frame->map_offset = 0;
156 frame->map_name = NULL;
157 frame->map_offset = 0;
158
159 frame->func_offset = 0;
160 frame->func_name = NULL;
161 }
162 }
163}
164
165//-------------------------------------------------------------------------
166// CorkscrewPtrace functions.
167//-------------------------------------------------------------------------
168CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) {
169}
170
171CorkscrewPtrace::~CorkscrewPtrace() {
172 if (ptrace_context_) {
173 free_ptrace_context(ptrace_context_);
174 ptrace_context_ = NULL;
175 }
176}
177
178bool CorkscrewPtrace::Unwind(size_t num_ignore_frames) {
179 ptrace_context_ = load_ptrace_context(backtrace_obj_->Tid());
180
181 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
182 ssize_t num_frames = unwind_backtrace_ptrace(
183 backtrace_obj_->Tid(), ptrace_context_, frames, num_ignore_frames,
184 MAX_BACKTRACE_FRAMES);
185
186 return GenerateFrameData(frames, num_frames);
187}
188
189std::string CorkscrewPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
190 // Get information about a different process.
191 const map_info_t* map_info;
192 const symbol_t* symbol;
193 find_symbol_ptrace(ptrace_context_, pc, &map_info, &symbol);
194 char* symbol_name = NULL;
195 if (symbol) {
196 if (map_info) {
197 *offset = pc - map_info->start - symbol->start;
198 }
199 symbol_name = symbol->name;
200 return symbol_name;
201 }
202
203 return "";
204}
205
206//-------------------------------------------------------------------------
Christopher Ferris8ed46272013-10-29 15:44:25 -0700207// C++ object creation functions.
Christopher Ferris17e91d42013-10-21 13:30:52 -0700208//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800209Backtrace* CreateCurrentObj(backtrace_map_info_t* map_info) {
210 return new BacktraceCurrent(new CorkscrewCurrent(), map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700211}
212
Christopher Ferris98464972014-01-06 19:16:33 -0800213Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, backtrace_map_info_t* map_info) {
214 return new BacktracePtrace(new CorkscrewPtrace(), pid, tid, map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700215}
216
Christopher Ferris98464972014-01-06 19:16:33 -0800217Backtrace* CreateThreadObj(pid_t tid, backtrace_map_info_t* map_info) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700218 CorkscrewThread* thread_obj = new CorkscrewThread();
Christopher Ferris98464972014-01-06 19:16:33 -0800219 return new BacktraceThread(thread_obj, thread_obj, tid, map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700220}