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 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 68 | // The maps are in descending order, but we want them in ascending order. |
| 69 | maps_.push_front(map); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 70 | |
| 71 | cur_map = cur_map->next; |
| 72 | } |
| 73 | return map_info_ != NULL; |
| 74 | } |
| 75 | |
| 76 | //------------------------------------------------------------------------- |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 77 | // CorkscrewCommon functions. |
| 78 | //------------------------------------------------------------------------- |
| 79 | bool CorkscrewCommon::GenerateFrameData( |
| 80 | backtrace_frame_t* cork_frames, ssize_t num_frames) { |
| 81 | if (num_frames < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 82 | BACK_LOGW("libcorkscrew unwind failed."); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 86 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 96 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 97 | it->map = FindMap(it->pc); |
| 98 | it->func_name = GetFunctionName(it->pc, &it->func_offset); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | //------------------------------------------------------------------------- |
| 104 | // CorkscrewCurrent functions. |
| 105 | //------------------------------------------------------------------------- |
| 106 | CorkscrewCurrent::CorkscrewCurrent() { |
| 107 | } |
| 108 | |
| 109 | CorkscrewCurrent::~CorkscrewCurrent() { |
| 110 | } |
| 111 | |
| 112 | bool 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 | |
| 119 | std::string CorkscrewCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) { |
| 120 | *offset = 0; |
| 121 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 122 | Dl_info info; |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 123 | const backtrace_map_t* map = FindMap(pc); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 124 | if (map) { |
Christopher Ferris | 923b536 | 2013-11-04 14:38:07 -0800 | [diff] [blame] | 125 | if (dladdr((const void*)pc, &info)) { |
| 126 | if (info.dli_sname) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 127 | *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] | 128 | 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 Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 132 | symbol_table_t* symbol_table = load_symbol_table(map->name.c_str()); |
Christopher Ferris | 923b536 | 2013-11-04 14:38:07 -0800 | [diff] [blame] | 133 | if (symbol_table) { |
| 134 | // First check if we can find the symbol using a relative pc. |
| 135 | std::string name; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 136 | const symbol_t* elf_symbol = find_symbol(symbol_table, pc - map->start); |
Christopher Ferris | 923b536 | 2013-11-04 14:38:07 -0800 | [diff] [blame] | 137 | if (elf_symbol) { |
| 138 | name = elf_symbol->name; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 139 | *offset = pc - map->start - elf_symbol->start; |
Christopher Ferris | 923b536 | 2013-11-04 14:38:07 -0800 | [diff] [blame] | 140 | } 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 149 | } |
| 150 | return ""; |
| 151 | } |
| 152 | |
| 153 | //------------------------------------------------------------------------- |
| 154 | // CorkscrewThread functions. |
| 155 | //------------------------------------------------------------------------- |
| 156 | CorkscrewThread::CorkscrewThread() { |
| 157 | } |
| 158 | |
| 159 | CorkscrewThread::~CorkscrewThread() { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 162 | void CorkscrewThread::ThreadUnwind( |
| 163 | siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 164 | backtrace_frame_t cork_frames[MAX_BACKTRACE_FRAMES]; |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 165 | CorkscrewMap* map = static_cast<CorkscrewMap*>(GetMap()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 166 | ssize_t num_frames = unwind_backtrace_signal_arch( |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 167 | siginfo, sigcontext, map->GetMapInfo(), cork_frames, |
| 168 | num_ignore_frames, MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 169 | if (num_frames > 0) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 170 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | //------------------------------------------------------------------------- |
| 186 | // CorkscrewPtrace functions. |
| 187 | //------------------------------------------------------------------------- |
| 188 | CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) { |
| 189 | } |
| 190 | |
| 191 | CorkscrewPtrace::~CorkscrewPtrace() { |
| 192 | if (ptrace_context_) { |
| 193 | free_ptrace_context(ptrace_context_); |
| 194 | ptrace_context_ = NULL; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | bool CorkscrewPtrace::Unwind(size_t num_ignore_frames) { |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 199 | ptrace_context_ = load_ptrace_context(Tid()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 200 | |
| 201 | backtrace_frame_t frames[MAX_BACKTRACE_FRAMES]; |
| 202 | ssize_t num_frames = unwind_backtrace_ptrace( |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 203 | Tid(), ptrace_context_, frames, num_ignore_frames, MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 204 | |
| 205 | return GenerateFrameData(frames, num_frames); |
| 206 | } |
| 207 | |
| 208 | std::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 Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 226 | // C++ object creation functions. |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 227 | //------------------------------------------------------------------------- |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 228 | Backtrace* CreateCurrentObj(BacktraceMap* map) { |
| 229 | return new BacktraceCurrent(new CorkscrewCurrent(), map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 232 | Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) { |
| 233 | return new BacktracePtrace(new CorkscrewPtrace(), pid, tid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 236 | Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 237 | CorkscrewThread* thread_obj = new CorkscrewThread(); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 238 | return new BacktraceThread(thread_obj, thread_obj, tid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 239 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 240 | |
| 241 | //------------------------------------------------------------------------- |
| 242 | // BacktraceMap create function. |
| 243 | //------------------------------------------------------------------------- |
| 244 | BacktraceMap* 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 | } |