blob: efeee2ef8a995e4522eb4250b857ff3e856c6a6b [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
Christopher Ferrisdf290612014-01-22 19:21:07 -080068 // The maps are in descending order, but we want them in ascending order.
69 maps_.push_front(map);
Christopher Ferris46756822014-01-14 20:16:30 -080070
71 cur_map = cur_map->next;
72 }
73 return map_info_ != NULL;
74}
75
76//-------------------------------------------------------------------------
Christopher Ferris17e91d42013-10-21 13:30:52 -070077// CorkscrewCommon functions.
78//-------------------------------------------------------------------------
79bool CorkscrewCommon::GenerateFrameData(
80 backtrace_frame_t* cork_frames, ssize_t num_frames) {
81 if (num_frames < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070082 BACK_LOGW("libcorkscrew unwind failed.");
Christopher Ferris17e91d42013-10-21 13:30:52 -070083 return false;
84 }
85
Christopher Ferris46756822014-01-14 20:16:30 -080086 std::vector<backtrace_frame_data_t>* frames = GetFrames();
87 frames->resize(num_frames);
88 size_t i = 0;
89 for (std::vector<backtrace_frame_data_t>::iterator it = frames->begin();
90 it != frames->end(); ++it, ++i) {
91 it->num = i;
92 it->pc = cork_frames[i].absolute_pc;
93 it->sp = cork_frames[i].stack_top;
94 it->stack_size = cork_frames[i].stack_size;
95 it->func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070096
Christopher Ferrisdf290612014-01-22 19:21:07 -080097 it->map = FindMap(it->pc);
98 it->func_name = GetFunctionName(it->pc, &it->func_offset);
Christopher Ferris17e91d42013-10-21 13:30:52 -070099 }
100 return true;
101}
102
103//-------------------------------------------------------------------------
104// CorkscrewCurrent functions.
105//-------------------------------------------------------------------------
106CorkscrewCurrent::CorkscrewCurrent() {
107}
108
109CorkscrewCurrent::~CorkscrewCurrent() {
110}
111
112bool CorkscrewCurrent::Unwind(size_t num_ignore_frames) {
113 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
114 ssize_t num_frames = unwind_backtrace(frames, num_ignore_frames, MAX_BACKTRACE_FRAMES);
115
116 return GenerateFrameData(frames, num_frames);
117}
118
119std::string CorkscrewCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
120 *offset = 0;
121
Christopher Ferris17e91d42013-10-21 13:30:52 -0700122 Dl_info info;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800123 const backtrace_map_t* map = FindMap(pc);
Christopher Ferris46756822014-01-14 20:16:30 -0800124 if (map) {
Christopher Ferris923b5362013-11-04 14:38:07 -0800125 if (dladdr((const void*)pc, &info)) {
126 if (info.dli_sname) {
Christopher Ferris46756822014-01-14 20:16:30 -0800127 *offset = pc - map->start - (uintptr_t)info.dli_saddr + (uintptr_t)info.dli_fbase;
Christopher Ferris923b5362013-11-04 14:38:07 -0800128 return info.dli_sname;
129 }
130 } else {
131 // dladdr(3) didn't find a symbol; maybe it's static? Look in the ELF file...
Christopher Ferris46756822014-01-14 20:16:30 -0800132 symbol_table_t* symbol_table = load_symbol_table(map->name.c_str());
Christopher Ferris923b5362013-11-04 14:38:07 -0800133 if (symbol_table) {
134 // First check if we can find the symbol using a relative pc.
135 std::string name;
Christopher Ferris46756822014-01-14 20:16:30 -0800136 const symbol_t* elf_symbol = find_symbol(symbol_table, pc - map->start);
Christopher Ferris923b5362013-11-04 14:38:07 -0800137 if (elf_symbol) {
138 name = elf_symbol->name;
Christopher Ferris46756822014-01-14 20:16:30 -0800139 *offset = pc - map->start - elf_symbol->start;
Christopher Ferris923b5362013-11-04 14:38:07 -0800140 } else if ((elf_symbol = find_symbol(symbol_table, pc)) != NULL) {
141 // Found the symbol using the absolute pc.
142 name = elf_symbol->name;
143 *offset = pc - elf_symbol->start;
144 }
145 free_symbol_table(symbol_table);
146 return name;
147 }
148 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700149 }
150 return "";
151}
152
153//-------------------------------------------------------------------------
154// CorkscrewThread functions.
155//-------------------------------------------------------------------------
156CorkscrewThread::CorkscrewThread() {
157}
158
159CorkscrewThread::~CorkscrewThread() {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700160}
161
Christopher Ferris17e91d42013-10-21 13:30:52 -0700162void CorkscrewThread::ThreadUnwind(
163 siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) {
Christopher Ferris46756822014-01-14 20:16:30 -0800164 backtrace_frame_t cork_frames[MAX_BACKTRACE_FRAMES];
Christopher Ferrisdf290612014-01-22 19:21:07 -0800165 CorkscrewMap* map = static_cast<CorkscrewMap*>(GetMap());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700166 ssize_t num_frames = unwind_backtrace_signal_arch(
Christopher Ferris46756822014-01-14 20:16:30 -0800167 siginfo, sigcontext, map->GetMapInfo(), cork_frames,
168 num_ignore_frames, MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700169 if (num_frames > 0) {
Christopher Ferris46756822014-01-14 20:16:30 -0800170 std::vector<backtrace_frame_data_t>* frames = GetFrames();
171 frames->resize(num_frames);
172 size_t i = 0;
173 for (std::vector<backtrace_frame_data_t>::iterator it = frames->begin();
174 it != frames->end(); ++it, ++i) {
175 it->num = i;
176 it->pc = cork_frames[i].absolute_pc;
177 it->sp = cork_frames[i].stack_top;
178 it->stack_size = cork_frames[i].stack_size;
179 it->map = NULL;
180 it->func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700181 }
182 }
183}
184
185//-------------------------------------------------------------------------
186// CorkscrewPtrace functions.
187//-------------------------------------------------------------------------
188CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) {
189}
190
191CorkscrewPtrace::~CorkscrewPtrace() {
192 if (ptrace_context_) {
193 free_ptrace_context(ptrace_context_);
194 ptrace_context_ = NULL;
195 }
196}
197
198bool CorkscrewPtrace::Unwind(size_t num_ignore_frames) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800199 ptrace_context_ = load_ptrace_context(Tid());
Christopher Ferris17e91d42013-10-21 13:30:52 -0700200
201 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
202 ssize_t num_frames = unwind_backtrace_ptrace(
Christopher Ferrisdf290612014-01-22 19:21:07 -0800203 Tid(), ptrace_context_, frames, num_ignore_frames, MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700204
205 return GenerateFrameData(frames, num_frames);
206}
207
208std::string CorkscrewPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
209 // Get information about a different process.
210 const map_info_t* map_info;
211 const symbol_t* symbol;
212 find_symbol_ptrace(ptrace_context_, pc, &map_info, &symbol);
213 char* symbol_name = NULL;
214 if (symbol) {
215 if (map_info) {
216 *offset = pc - map_info->start - symbol->start;
217 }
218 symbol_name = symbol->name;
219 return symbol_name;
220 }
221
222 return "";
223}
224
225//-------------------------------------------------------------------------
Christopher Ferris8ed46272013-10-29 15:44:25 -0700226// C++ object creation functions.
Christopher Ferris17e91d42013-10-21 13:30:52 -0700227//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800228Backtrace* CreateCurrentObj(BacktraceMap* map) {
229 return new BacktraceCurrent(new CorkscrewCurrent(), map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700230}
231
Christopher Ferris46756822014-01-14 20:16:30 -0800232Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) {
233 return new BacktracePtrace(new CorkscrewPtrace(), pid, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700234}
235
Christopher Ferris46756822014-01-14 20:16:30 -0800236Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700237 CorkscrewThread* thread_obj = new CorkscrewThread();
Christopher Ferris46756822014-01-14 20:16:30 -0800238 return new BacktraceThread(thread_obj, thread_obj, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700239}
Christopher Ferrisdf290612014-01-22 19:21:07 -0800240
241//-------------------------------------------------------------------------
242// BacktraceMap create function.
243//-------------------------------------------------------------------------
244BacktraceMap* BacktraceMap::Create(pid_t pid) {
245 BacktraceMap* map = new CorkscrewMap(pid);
246 if (!map->Build()) {
247 delete map;
248 return NULL;
249 }
250 return map;
251}