blob: eae13c62f01686c06d3f8320b8275553b589977f [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];
48 frame->pc = cork_frames[i].absolute_pc;
49 frame->sp = cork_frames[i].stack_top;
50 frame->stack_size = cork_frames[i].stack_size;
51 frame->map_name = NULL;
52 frame->map_offset = 0;
53 frame->func_name = NULL;
54 frame->func_offset = 0;
55
56 uintptr_t map_start;
57 frame->map_name = backtrace_obj_->GetMapName(frame->pc, &map_start);
58 if (frame->map_name) {
59 frame->map_offset = frame->pc - map_start;
60 }
61
62 std::string func_name = backtrace_obj_->GetFunctionName(frame->pc, &frame->func_offset);
63 if (!func_name.empty()) {
64 frame->func_name = strdup(func_name.c_str());
65 }
66 }
67 return true;
68}
69
70//-------------------------------------------------------------------------
71// CorkscrewCurrent functions.
72//-------------------------------------------------------------------------
73CorkscrewCurrent::CorkscrewCurrent() {
74}
75
76CorkscrewCurrent::~CorkscrewCurrent() {
77}
78
79bool CorkscrewCurrent::Unwind(size_t num_ignore_frames) {
80 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
81 ssize_t num_frames = unwind_backtrace(frames, num_ignore_frames, MAX_BACKTRACE_FRAMES);
82
83 return GenerateFrameData(frames, num_frames);
84}
85
86std::string CorkscrewCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
87 *offset = 0;
88
Christopher Ferris17e91d42013-10-21 13:30:52 -070089 Dl_info info;
90 const backtrace_map_info_t* map_info = backtrace_obj_->FindMapInfo(pc);
Christopher Ferris923b5362013-11-04 14:38:07 -080091 if (map_info) {
92 if (dladdr((const void*)pc, &info)) {
93 if (info.dli_sname) {
94 *offset = pc - map_info->start - (uintptr_t)info.dli_saddr + (uintptr_t)info.dli_fbase;
95 return info.dli_sname;
96 }
97 } else {
98 // dladdr(3) didn't find a symbol; maybe it's static? Look in the ELF file...
99 symbol_table_t* symbol_table = load_symbol_table(map_info->name);
100 if (symbol_table) {
101 // First check if we can find the symbol using a relative pc.
102 std::string name;
103 const symbol_t* elf_symbol = find_symbol(symbol_table, pc - map_info->start);
104 if (elf_symbol) {
105 name = elf_symbol->name;
106 *offset = pc - map_info->start - elf_symbol->start;
107 } else if ((elf_symbol = find_symbol(symbol_table, pc)) != NULL) {
108 // Found the symbol using the absolute pc.
109 name = elf_symbol->name;
110 *offset = pc - elf_symbol->start;
111 }
112 free_symbol_table(symbol_table);
113 return name;
114 }
115 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700116 }
117 return "";
118}
119
120//-------------------------------------------------------------------------
121// CorkscrewThread functions.
122//-------------------------------------------------------------------------
123CorkscrewThread::CorkscrewThread() {
124}
125
126CorkscrewThread::~CorkscrewThread() {
127 if (corkscrew_map_info_) {
128 free_map_info_list(corkscrew_map_info_);
129 corkscrew_map_info_ = NULL;
130 }
131}
132
133bool CorkscrewThread::Init() {
134 corkscrew_map_info_ = load_map_info_list(backtrace_obj_->Pid());
135 return corkscrew_map_info_ != NULL;
136}
137
138void CorkscrewThread::ThreadUnwind(
139 siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) {
140 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
141 ssize_t num_frames = unwind_backtrace_signal_arch(
142 siginfo, sigcontext, corkscrew_map_info_, frames, num_ignore_frames,
143 MAX_BACKTRACE_FRAMES);
144 if (num_frames > 0) {
145 backtrace_t* data = GetBacktraceData();
146 data->num_frames = num_frames;
147 for (size_t i = 0; i < data->num_frames; i++) {
148 backtrace_frame_data_t* frame = &data->frames[i];
149 frame->pc = frames[i].absolute_pc;
150 frame->sp = frames[i].stack_top;
151 frame->stack_size = frames[i].stack_size;
152
153 frame->map_offset = 0;
154 frame->map_name = NULL;
155 frame->map_offset = 0;
156
157 frame->func_offset = 0;
158 frame->func_name = NULL;
159 }
160 }
161}
162
163//-------------------------------------------------------------------------
164// CorkscrewPtrace functions.
165//-------------------------------------------------------------------------
166CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) {
167}
168
169CorkscrewPtrace::~CorkscrewPtrace() {
170 if (ptrace_context_) {
171 free_ptrace_context(ptrace_context_);
172 ptrace_context_ = NULL;
173 }
174}
175
176bool CorkscrewPtrace::Unwind(size_t num_ignore_frames) {
177 ptrace_context_ = load_ptrace_context(backtrace_obj_->Tid());
178
179 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
180 ssize_t num_frames = unwind_backtrace_ptrace(
181 backtrace_obj_->Tid(), ptrace_context_, frames, num_ignore_frames,
182 MAX_BACKTRACE_FRAMES);
183
184 return GenerateFrameData(frames, num_frames);
185}
186
187std::string CorkscrewPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
188 // Get information about a different process.
189 const map_info_t* map_info;
190 const symbol_t* symbol;
191 find_symbol_ptrace(ptrace_context_, pc, &map_info, &symbol);
192 char* symbol_name = NULL;
193 if (symbol) {
194 if (map_info) {
195 *offset = pc - map_info->start - symbol->start;
196 }
197 symbol_name = symbol->name;
198 return symbol_name;
199 }
200
201 return "";
202}
203
204//-------------------------------------------------------------------------
Christopher Ferris8ed46272013-10-29 15:44:25 -0700205// C++ object creation functions.
Christopher Ferris17e91d42013-10-21 13:30:52 -0700206//-------------------------------------------------------------------------
Christopher Ferris98464972014-01-06 19:16:33 -0800207Backtrace* CreateCurrentObj(backtrace_map_info_t* map_info) {
208 return new BacktraceCurrent(new CorkscrewCurrent(), map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700209}
210
Christopher Ferris98464972014-01-06 19:16:33 -0800211Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, backtrace_map_info_t* map_info) {
212 return new BacktracePtrace(new CorkscrewPtrace(), pid, tid, map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700213}
214
Christopher Ferris98464972014-01-06 19:16:33 -0800215Backtrace* CreateThreadObj(pid_t tid, backtrace_map_info_t* map_info) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700216 CorkscrewThread* thread_obj = new CorkscrewThread();
Christopher Ferris98464972014-01-06 19:16:33 -0800217 return new BacktraceThread(thread_obj, thread_obj, tid, map_info);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700218}