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 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 17 | #include <backtrace/Backtrace.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include <backtrace-arch.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 22 | #include <corkscrew/backtrace.h> |
| 23 | |
| 24 | #ifndef __USE_GNU |
| 25 | #define __USE_GNU |
| 26 | #endif |
| 27 | #include <dlfcn.h> |
| 28 | |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 29 | #include "BacktraceLog.h" |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 30 | #include "Corkscrew.h" |
| 31 | |
| 32 | //------------------------------------------------------------------------- |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 33 | // CorkscrewMap functions. |
| 34 | //------------------------------------------------------------------------- |
| 35 | CorkscrewMap::CorkscrewMap(pid_t pid) : BacktraceMap(pid), map_info_(NULL) { |
| 36 | } |
| 37 | |
| 38 | CorkscrewMap::~CorkscrewMap() { |
| 39 | if (map_info_) { |
| 40 | free_map_info_list(map_info_); |
| 41 | map_info_ = NULL; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | bool 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 Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 67 | // The maps are in descending order, but we want them in ascending order. |
| 68 | maps_.push_front(map); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 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 | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 96 | it->map = FindMap(it->pc); |
| 97 | it->func_name = 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 | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 122 | const backtrace_map_t* map = FindMap(pc); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 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 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 161 | void CorkscrewThread::ThreadUnwind( |
| 162 | siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 163 | backtrace_frame_t cork_frames[MAX_BACKTRACE_FRAMES]; |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 164 | CorkscrewMap* map = static_cast<CorkscrewMap*>(GetMap()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 165 | ssize_t num_frames = unwind_backtrace_signal_arch( |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 166 | siginfo, sigcontext, map->GetMapInfo(), cork_frames, |
| 167 | num_ignore_frames, MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 168 | if (num_frames > 0) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 169 | 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 Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | //------------------------------------------------------------------------- |
| 185 | // CorkscrewPtrace functions. |
| 186 | //------------------------------------------------------------------------- |
| 187 | CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) { |
| 188 | } |
| 189 | |
| 190 | CorkscrewPtrace::~CorkscrewPtrace() { |
| 191 | if (ptrace_context_) { |
| 192 | free_ptrace_context(ptrace_context_); |
| 193 | ptrace_context_ = NULL; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | bool CorkscrewPtrace::Unwind(size_t num_ignore_frames) { |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 198 | ptrace_context_ = load_ptrace_context(Tid()); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 199 | |
| 200 | backtrace_frame_t frames[MAX_BACKTRACE_FRAMES]; |
| 201 | ssize_t num_frames = unwind_backtrace_ptrace( |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 202 | Tid(), ptrace_context_, frames, num_ignore_frames, MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 203 | |
| 204 | return GenerateFrameData(frames, num_frames); |
| 205 | } |
| 206 | |
| 207 | std::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 Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 225 | // C++ object creation functions. |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 226 | //------------------------------------------------------------------------- |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 227 | Backtrace* CreateCurrentObj(BacktraceMap* map) { |
| 228 | return new BacktraceCurrent(new CorkscrewCurrent(), map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 231 | Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) { |
| 232 | return new BacktracePtrace(new CorkscrewPtrace(), pid, tid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 235 | Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) { |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 236 | CorkscrewThread* thread_obj = new CorkscrewThread(); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 237 | return new BacktraceThread(thread_obj, thread_obj, tid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 238 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 239 | |
| 240 | //------------------------------------------------------------------------- |
| 241 | // BacktraceMap create function. |
| 242 | //------------------------------------------------------------------------- |
| 243 | BacktraceMap* 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 | } |