blob: fd31a2612857d3c06bf65630e66f94edfe317d84 [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
Christopher Ferris46756822014-01-14 20:16:30 -080019#include <backtrace/Backtrace.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070020
21#include <string.h>
22
23#include <backtrace-arch.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070024#include <corkscrew/backtrace.h>
25
26#ifndef __USE_GNU
27#define __USE_GNU
28#endif
29#include <dlfcn.h>
30
31#include "Corkscrew.h"
32
33//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -080034// CorkscrewMap functions.
35//-------------------------------------------------------------------------
36CorkscrewMap::CorkscrewMap(pid_t pid) : BacktraceMap(pid), map_info_(NULL) {
37}
38
39CorkscrewMap::~CorkscrewMap() {
40 if (map_info_) {
41 free_map_info_list(map_info_);
42 map_info_ = NULL;
43 }
44}
45
46bool CorkscrewMap::Build() {
47 map_info_ = load_map_info_list(pid_);
48
49 // Use the information in map_info_ to construct the BacktraceMap data
50 // rather than reparsing /proc/self/maps.
51 map_info_t* cur_map = map_info_;
52 while (cur_map) {
53 backtrace_map_t map;
54 map.start = cur_map->start;
55 map.end = cur_map->end;
56 map.flags = 0;
57 if (cur_map->is_readable) {
58 map.flags |= PROT_READ;
59 }
60 if (cur_map->is_writable) {
61 map.flags |= PROT_WRITE;
62 }
63 if (cur_map->is_executable) {
64 map.flags |= PROT_EXEC;
65 }
66 map.name = cur_map->name;
67
68 maps_.push_back(map);
69
70 cur_map = cur_map->next;
71 }
72 return map_info_ != NULL;
73}
74
75//-------------------------------------------------------------------------
Christopher Ferris17e91d42013-10-21 13:30:52 -070076// CorkscrewCommon functions.
77//-------------------------------------------------------------------------
78bool CorkscrewCommon::GenerateFrameData(
79 backtrace_frame_t* cork_frames, ssize_t num_frames) {
80 if (num_frames < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070081 BACK_LOGW("libcorkscrew unwind failed.");
Christopher Ferris17e91d42013-10-21 13:30:52 -070082 return false;
83 }
84
Christopher Ferris46756822014-01-14 20:16:30 -080085 std::vector<backtrace_frame_data_t>* frames = GetFrames();
86 frames->resize(num_frames);
87 size_t i = 0;
88 for (std::vector<backtrace_frame_data_t>::iterator it = frames->begin();
89 it != frames->end(); ++it, ++i) {
90 it->num = i;
91 it->pc = cork_frames[i].absolute_pc;
92 it->sp = cork_frames[i].stack_top;
93 it->stack_size = cork_frames[i].stack_size;
94 it->func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070095
Christopher Ferris46756822014-01-14 20:16:30 -080096 it->map = backtrace_obj_->FindMap(it->pc);
97 it->func_name = backtrace_obj_->GetFunctionName(it->pc, &it->func_offset);
Christopher Ferris17e91d42013-10-21 13:30:52 -070098 }
99 return true;
100}
101
102//-------------------------------------------------------------------------
103// CorkscrewCurrent functions.
104//-------------------------------------------------------------------------
105CorkscrewCurrent::CorkscrewCurrent() {
106}
107
108CorkscrewCurrent::~CorkscrewCurrent() {
109}
110
111bool CorkscrewCurrent::Unwind(size_t num_ignore_frames) {
112 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
113 ssize_t num_frames = unwind_backtrace(frames, num_ignore_frames, MAX_BACKTRACE_FRAMES);
114
115 return GenerateFrameData(frames, num_frames);
116}
117
118std::string CorkscrewCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
119 *offset = 0;
120
Christopher Ferris17e91d42013-10-21 13:30:52 -0700121 Dl_info info;
Christopher Ferris46756822014-01-14 20:16:30 -0800122 const backtrace_map_t* map = backtrace_obj_->FindMap(pc);
123 if (map) {
Christopher Ferris923b5362013-11-04 14:38:07 -0800124 if (dladdr((const void*)pc, &info)) {
125 if (info.dli_sname) {
Christopher Ferris46756822014-01-14 20:16:30 -0800126 *offset = pc - map->start - (uintptr_t)info.dli_saddr + (uintptr_t)info.dli_fbase;
Christopher Ferris923b5362013-11-04 14:38:07 -0800127 return info.dli_sname;
128 }
129 } else {
130 // dladdr(3) didn't find a symbol; maybe it's static? Look in the ELF file...
Christopher Ferris46756822014-01-14 20:16:30 -0800131 symbol_table_t* symbol_table = load_symbol_table(map->name.c_str());
Christopher Ferris923b5362013-11-04 14:38:07 -0800132 if (symbol_table) {
133 // First check if we can find the symbol using a relative pc.
134 std::string name;
Christopher Ferris46756822014-01-14 20:16:30 -0800135 const symbol_t* elf_symbol = find_symbol(symbol_table, pc - map->start);
Christopher Ferris923b5362013-11-04 14:38:07 -0800136 if (elf_symbol) {
137 name = elf_symbol->name;
Christopher Ferris46756822014-01-14 20:16:30 -0800138 *offset = pc - map->start - elf_symbol->start;
Christopher Ferris923b5362013-11-04 14:38:07 -0800139 } else if ((elf_symbol = find_symbol(symbol_table, pc)) != NULL) {
140 // Found the symbol using the absolute pc.
141 name = elf_symbol->name;
142 *offset = pc - elf_symbol->start;
143 }
144 free_symbol_table(symbol_table);
145 return name;
146 }
147 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700148 }
149 return "";
150}
151
152//-------------------------------------------------------------------------
153// CorkscrewThread functions.
154//-------------------------------------------------------------------------
155CorkscrewThread::CorkscrewThread() {
156}
157
158CorkscrewThread::~CorkscrewThread() {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700159}
160
161bool CorkscrewThread::Init() {
Christopher Ferris46756822014-01-14 20:16:30 -0800162 if (backtrace_obj_->GetMap() == NULL) {
163 // Trigger the map object creation, which will create the corkscrew
164 // map information.
165 return BuildMap();
166 }
167 return true;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700168}
169
170void CorkscrewThread::ThreadUnwind(
171 siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) {
Christopher Ferris46756822014-01-14 20:16:30 -0800172 backtrace_frame_t cork_frames[MAX_BACKTRACE_FRAMES];
173 CorkscrewMap* map = static_cast<CorkscrewMap*>(backtrace_obj_->GetMap());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700174 ssize_t num_frames = unwind_backtrace_signal_arch(
Christopher Ferris46756822014-01-14 20:16:30 -0800175 siginfo, sigcontext, map->GetMapInfo(), cork_frames,
176 num_ignore_frames, MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700177 if (num_frames > 0) {
Christopher Ferris46756822014-01-14 20:16:30 -0800178 std::vector<backtrace_frame_data_t>* frames = GetFrames();
179 frames->resize(num_frames);
180 size_t i = 0;
181 for (std::vector<backtrace_frame_data_t>::iterator it = frames->begin();
182 it != frames->end(); ++it, ++i) {
183 it->num = i;
184 it->pc = cork_frames[i].absolute_pc;
185 it->sp = cork_frames[i].stack_top;
186 it->stack_size = cork_frames[i].stack_size;
187 it->map = NULL;
188 it->func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700189 }
190 }
191}
192
193//-------------------------------------------------------------------------
194// CorkscrewPtrace functions.
195//-------------------------------------------------------------------------
196CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) {
197}
198
199CorkscrewPtrace::~CorkscrewPtrace() {
200 if (ptrace_context_) {
201 free_ptrace_context(ptrace_context_);
202 ptrace_context_ = NULL;
203 }
204}
205
206bool CorkscrewPtrace::Unwind(size_t num_ignore_frames) {
207 ptrace_context_ = load_ptrace_context(backtrace_obj_->Tid());
208
209 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
210 ssize_t num_frames = unwind_backtrace_ptrace(
211 backtrace_obj_->Tid(), ptrace_context_, frames, num_ignore_frames,
212 MAX_BACKTRACE_FRAMES);
213
214 return GenerateFrameData(frames, num_frames);
215}
216
217std::string CorkscrewPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
218 // Get information about a different process.
219 const map_info_t* map_info;
220 const symbol_t* symbol;
221 find_symbol_ptrace(ptrace_context_, pc, &map_info, &symbol);
222 char* symbol_name = NULL;
223 if (symbol) {
224 if (map_info) {
225 *offset = pc - map_info->start - symbol->start;
226 }
227 symbol_name = symbol->name;
228 return symbol_name;
229 }
230
231 return "";
232}
233
234//-------------------------------------------------------------------------
Christopher Ferris8ed46272013-10-29 15:44:25 -0700235// C++ object creation functions.
Christopher Ferris17e91d42013-10-21 13:30:52 -0700236//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800237Backtrace* CreateCurrentObj(BacktraceMap* map) {
238 return new BacktraceCurrent(new CorkscrewCurrent(), map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700239}
240
Christopher Ferris46756822014-01-14 20:16:30 -0800241Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) {
242 return new BacktracePtrace(new CorkscrewPtrace(), pid, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700243}
244
Christopher Ferris46756822014-01-14 20:16:30 -0800245Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700246 CorkscrewThread* thread_obj = new CorkscrewThread();
Christopher Ferris46756822014-01-14 20:16:30 -0800247 return new BacktraceThread(thread_obj, thread_obj, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700248}