blob: 773b0a259c0a35bf0a43b3121fcd3e3d85d7dad2 [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
Christopher Ferris46756822014-01-14 20:16:30 -080017#include <backtrace/Backtrace.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070018
19#include <string.h>
20
21#include <backtrace-arch.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070022#include <corkscrew/backtrace.h>
23
24#ifndef __USE_GNU
25#define __USE_GNU
26#endif
27#include <dlfcn.h>
28
Christopher Ferrise2960912014-03-07 19:42:19 -080029#include "BacktraceLog.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070030#include "Corkscrew.h"
31
32//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -080033// CorkscrewMap functions.
34//-------------------------------------------------------------------------
35CorkscrewMap::CorkscrewMap(pid_t pid) : BacktraceMap(pid), map_info_(NULL) {
36}
37
38CorkscrewMap::~CorkscrewMap() {
39 if (map_info_) {
40 free_map_info_list(map_info_);
41 map_info_ = NULL;
42 }
43}
44
45bool CorkscrewMap::Build() {
46 map_info_ = load_map_info_list(pid_);
47
48 // Use the information in map_info_ to construct the BacktraceMap data
49 // rather than reparsing /proc/self/maps.
50 map_info_t* cur_map = map_info_;
51 while (cur_map) {
52 backtrace_map_t map;
53 map.start = cur_map->start;
54 map.end = cur_map->end;
55 map.flags = 0;
56 if (cur_map->is_readable) {
57 map.flags |= PROT_READ;
58 }
59 if (cur_map->is_writable) {
60 map.flags |= PROT_WRITE;
61 }
62 if (cur_map->is_executable) {
63 map.flags |= PROT_EXEC;
64 }
65 map.name = cur_map->name;
66
Christopher Ferrisdf290612014-01-22 19:21:07 -080067 // The maps are in descending order, but we want them in ascending order.
68 maps_.push_front(map);
Christopher Ferris46756822014-01-14 20:16:30 -080069
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 Ferrisdf290612014-01-22 19:21:07 -080096 it->map = FindMap(it->pc);
97 it->func_name = 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 Ferrisdf290612014-01-22 19:21:07 -0800122 const backtrace_map_t* map = FindMap(pc);
Christopher Ferris46756822014-01-14 20:16:30 -0800123 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
Christopher Ferris17e91d42013-10-21 13:30:52 -0700161void CorkscrewThread::ThreadUnwind(
162 siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) {
Christopher Ferris46756822014-01-14 20:16:30 -0800163 backtrace_frame_t cork_frames[MAX_BACKTRACE_FRAMES];
Christopher Ferrisdf290612014-01-22 19:21:07 -0800164 CorkscrewMap* map = static_cast<CorkscrewMap*>(GetMap());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700165 ssize_t num_frames = unwind_backtrace_signal_arch(
Christopher Ferris46756822014-01-14 20:16:30 -0800166 siginfo, sigcontext, map->GetMapInfo(), cork_frames,
167 num_ignore_frames, MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700168 if (num_frames > 0) {
Christopher Ferris46756822014-01-14 20:16:30 -0800169 std::vector<backtrace_frame_data_t>* frames = GetFrames();
170 frames->resize(num_frames);
171 size_t i = 0;
172 for (std::vector<backtrace_frame_data_t>::iterator it = frames->begin();
173 it != frames->end(); ++it, ++i) {
174 it->num = i;
175 it->pc = cork_frames[i].absolute_pc;
176 it->sp = cork_frames[i].stack_top;
177 it->stack_size = cork_frames[i].stack_size;
178 it->map = NULL;
179 it->func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700180 }
181 }
182}
183
184//-------------------------------------------------------------------------
185// CorkscrewPtrace functions.
186//-------------------------------------------------------------------------
187CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) {
188}
189
190CorkscrewPtrace::~CorkscrewPtrace() {
191 if (ptrace_context_) {
192 free_ptrace_context(ptrace_context_);
193 ptrace_context_ = NULL;
194 }
195}
196
197bool CorkscrewPtrace::Unwind(size_t num_ignore_frames) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800198 ptrace_context_ = load_ptrace_context(Tid());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700199
200 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
201 ssize_t num_frames = unwind_backtrace_ptrace(
Christopher Ferrisdf290612014-01-22 19:21:07 -0800202 Tid(), ptrace_context_, frames, num_ignore_frames, MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700203
204 return GenerateFrameData(frames, num_frames);
205}
206
207std::string CorkscrewPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
208 // Get information about a different process.
209 const map_info_t* map_info;
210 const symbol_t* symbol;
211 find_symbol_ptrace(ptrace_context_, pc, &map_info, &symbol);
212 char* symbol_name = NULL;
213 if (symbol) {
214 if (map_info) {
215 *offset = pc - map_info->start - symbol->start;
216 }
217 symbol_name = symbol->name;
218 return symbol_name;
219 }
220
221 return "";
222}
223
224//-------------------------------------------------------------------------
Christopher Ferris8ed46272013-10-29 15:44:25 -0700225// C++ object creation functions.
Christopher Ferris17e91d42013-10-21 13:30:52 -0700226//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800227Backtrace* CreateCurrentObj(BacktraceMap* map) {
228 return new BacktraceCurrent(new CorkscrewCurrent(), map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700229}
230
Christopher Ferris46756822014-01-14 20:16:30 -0800231Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) {
232 return new BacktracePtrace(new CorkscrewPtrace(), pid, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700233}
234
Christopher Ferris46756822014-01-14 20:16:30 -0800235Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700236 CorkscrewThread* thread_obj = new CorkscrewThread();
Christopher Ferris46756822014-01-14 20:16:30 -0800237 return new BacktraceThread(thread_obj, thread_obj, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700238}
Christopher Ferrisdf290612014-01-22 19:21:07 -0800239
240//-------------------------------------------------------------------------
241// BacktraceMap create function.
242//-------------------------------------------------------------------------
243BacktraceMap* BacktraceMap::Create(pid_t pid) {
244 BacktraceMap* map = new CorkscrewMap(pid);
245 if (!map->Build()) {
246 delete map;
247 return NULL;
248 }
249 return map;
250}