blob: 8ba1e8097ea247ca20177f4e27be88d1b655d894 [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) {
40 ALOGW("CorkscrewCommon::GenerateFrameData: libcorkscrew unwind failed.\n");
41 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
89 // Get information about the current thread.
90 Dl_info info;
91 const backtrace_map_info_t* map_info = backtrace_obj_->FindMapInfo(pc);
92 const char* symbol_name = NULL;
93 if (map_info && dladdr((const void*)pc, &info) && info.dli_sname) {
94 *offset = pc - map_info->start - (uintptr_t)info.dli_saddr + (uintptr_t)info.dli_fbase;
95 symbol_name = info.dli_sname;
96
97 return symbol_name;
98 }
99 return "";
100}
101
102//-------------------------------------------------------------------------
103// CorkscrewThread functions.
104//-------------------------------------------------------------------------
105CorkscrewThread::CorkscrewThread() {
106}
107
108CorkscrewThread::~CorkscrewThread() {
109 if (corkscrew_map_info_) {
110 free_map_info_list(corkscrew_map_info_);
111 corkscrew_map_info_ = NULL;
112 }
113}
114
115bool CorkscrewThread::Init() {
116 corkscrew_map_info_ = load_map_info_list(backtrace_obj_->Pid());
117 return corkscrew_map_info_ != NULL;
118}
119
120void CorkscrewThread::ThreadUnwind(
121 siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) {
122 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
123 ssize_t num_frames = unwind_backtrace_signal_arch(
124 siginfo, sigcontext, corkscrew_map_info_, frames, num_ignore_frames,
125 MAX_BACKTRACE_FRAMES);
126 if (num_frames > 0) {
127 backtrace_t* data = GetBacktraceData();
128 data->num_frames = num_frames;
129 for (size_t i = 0; i < data->num_frames; i++) {
130 backtrace_frame_data_t* frame = &data->frames[i];
131 frame->pc = frames[i].absolute_pc;
132 frame->sp = frames[i].stack_top;
133 frame->stack_size = frames[i].stack_size;
134
135 frame->map_offset = 0;
136 frame->map_name = NULL;
137 frame->map_offset = 0;
138
139 frame->func_offset = 0;
140 frame->func_name = NULL;
141 }
142 }
143}
144
145//-------------------------------------------------------------------------
146// CorkscrewPtrace functions.
147//-------------------------------------------------------------------------
148CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) {
149}
150
151CorkscrewPtrace::~CorkscrewPtrace() {
152 if (ptrace_context_) {
153 free_ptrace_context(ptrace_context_);
154 ptrace_context_ = NULL;
155 }
156}
157
158bool CorkscrewPtrace::Unwind(size_t num_ignore_frames) {
159 ptrace_context_ = load_ptrace_context(backtrace_obj_->Tid());
160
161 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
162 ssize_t num_frames = unwind_backtrace_ptrace(
163 backtrace_obj_->Tid(), ptrace_context_, frames, num_ignore_frames,
164 MAX_BACKTRACE_FRAMES);
165
166 return GenerateFrameData(frames, num_frames);
167}
168
169std::string CorkscrewPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
170 // Get information about a different process.
171 const map_info_t* map_info;
172 const symbol_t* symbol;
173 find_symbol_ptrace(ptrace_context_, pc, &map_info, &symbol);
174 char* symbol_name = NULL;
175 if (symbol) {
176 if (map_info) {
177 *offset = pc - map_info->start - symbol->start;
178 }
179 symbol_name = symbol->name;
180 return symbol_name;
181 }
182
183 return "";
184}
185
186//-------------------------------------------------------------------------
187// C++ object createion functions.
188//-------------------------------------------------------------------------
189Backtrace* CreateCurrentObj() {
190 return new BacktraceCurrent(new CorkscrewCurrent());
191}
192
193Backtrace* CreatePtraceObj(pid_t pid, pid_t tid) {
194 return new BacktracePtrace(new CorkscrewPtrace(), pid, tid);
195}
196
197Backtrace* CreateThreadObj(pid_t tid) {
198 CorkscrewThread* thread_obj = new CorkscrewThread();
199 return new BacktraceThread(thread_obj, thread_obj, tid);
200}