Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 19 | #include <backtrace/Backtrace.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <backtrace-arch.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 24 | #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 Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 34 | // CorkscrewMap functions. |
| 35 | //------------------------------------------------------------------------- |
| 36 | CorkscrewMap::CorkscrewMap(pid_t pid) : BacktraceMap(pid), map_info_(NULL) { |
| 37 | } |
| 38 | |
| 39 | CorkscrewMap::~CorkscrewMap() { |
| 40 | if (map_info_) { |
| 41 | free_map_info_list(map_info_); |
| 42 | map_info_ = NULL; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | bool 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 76 | // CorkscrewCommon functions. |
| 77 | //------------------------------------------------------------------------- |
| 78 | bool CorkscrewCommon::GenerateFrameData( |
| 79 | backtrace_frame_t* cork_frames, ssize_t num_frames) { |
| 80 | if (num_frames < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 81 | BACK_LOGW("libcorkscrew unwind failed."); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 85 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 95 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 96 | it->map = backtrace_obj_->FindMap(it->pc); |
| 97 | it->func_name = backtrace_obj_->GetFunctionName(it->pc, &it->func_offset); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | //------------------------------------------------------------------------- |
| 103 | // CorkscrewCurrent functions. |
| 104 | //------------------------------------------------------------------------- |
| 105 | CorkscrewCurrent::CorkscrewCurrent() { |
| 106 | } |
| 107 | |
| 108 | CorkscrewCurrent::~CorkscrewCurrent() { |
| 109 | } |
| 110 | |
| 111 | bool 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 | |
| 118 | std::string CorkscrewCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) { |
| 119 | *offset = 0; |
| 120 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 121 | Dl_info info; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 122 | const backtrace_map_t* map = backtrace_obj_->FindMap(pc); |
| 123 | if (map) { |
Christopher Ferris | 923b536 | 2013-11-04 14:38:07 -0800 | [diff] [blame] | 124 | if (dladdr((const void*)pc, &info)) { |
| 125 | if (info.dli_sname) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 126 | *offset = pc - map->start - (uintptr_t)info.dli_saddr + (uintptr_t)info.dli_fbase; |
Christopher Ferris | 923b536 | 2013-11-04 14:38:07 -0800 | [diff] [blame] | 127 | 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 Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 131 | symbol_table_t* symbol_table = load_symbol_table(map->name.c_str()); |
Christopher Ferris | 923b536 | 2013-11-04 14:38:07 -0800 | [diff] [blame] | 132 | if (symbol_table) { |
| 133 | // First check if we can find the symbol using a relative pc. |
| 134 | std::string name; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 135 | const symbol_t* elf_symbol = find_symbol(symbol_table, pc - map->start); |
Christopher Ferris | 923b536 | 2013-11-04 14:38:07 -0800 | [diff] [blame] | 136 | if (elf_symbol) { |
| 137 | name = elf_symbol->name; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 138 | *offset = pc - map->start - elf_symbol->start; |
Christopher Ferris | 923b536 | 2013-11-04 14:38:07 -0800 | [diff] [blame] | 139 | } 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 148 | } |
| 149 | return ""; |
| 150 | } |
| 151 | |
| 152 | //------------------------------------------------------------------------- |
| 153 | // CorkscrewThread functions. |
| 154 | //------------------------------------------------------------------------- |
| 155 | CorkscrewThread::CorkscrewThread() { |
| 156 | } |
| 157 | |
| 158 | CorkscrewThread::~CorkscrewThread() { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | bool CorkscrewThread::Init() { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 162 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void CorkscrewThread::ThreadUnwind( |
| 171 | siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 172 | backtrace_frame_t cork_frames[MAX_BACKTRACE_FRAMES]; |
| 173 | CorkscrewMap* map = static_cast<CorkscrewMap*>(backtrace_obj_->GetMap()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 174 | ssize_t num_frames = unwind_backtrace_signal_arch( |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 175 | siginfo, sigcontext, map->GetMapInfo(), cork_frames, |
| 176 | num_ignore_frames, MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 177 | if (num_frames > 0) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 178 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | //------------------------------------------------------------------------- |
| 194 | // CorkscrewPtrace functions. |
| 195 | //------------------------------------------------------------------------- |
| 196 | CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) { |
| 197 | } |
| 198 | |
| 199 | CorkscrewPtrace::~CorkscrewPtrace() { |
| 200 | if (ptrace_context_) { |
| 201 | free_ptrace_context(ptrace_context_); |
| 202 | ptrace_context_ = NULL; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | bool 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 | |
| 217 | std::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 Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 235 | // C++ object creation functions. |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 236 | //------------------------------------------------------------------------- |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 237 | Backtrace* CreateCurrentObj(BacktraceMap* map) { |
| 238 | return new BacktraceCurrent(new CorkscrewCurrent(), map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 241 | Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) { |
| 242 | return new BacktracePtrace(new CorkscrewPtrace(), pid, tid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 245 | Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 246 | CorkscrewThread* thread_obj = new CorkscrewThread(); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame^] | 247 | return new BacktraceThread(thread_obj, thread_obj, tid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 248 | } |