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> |
| 20 | #include <backtrace/BacktraceMap.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <string.h> |
| 24 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 25 | #include <libunwind.h> |
| 26 | #include <libunwind-ptrace.h> |
| 27 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame^] | 28 | #include "UnwindMap.h" |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 29 | #include "UnwindPtrace.h" |
| 30 | |
| 31 | UnwindPtrace::UnwindPtrace() : addr_space_(NULL), upt_info_(NULL) { |
| 32 | } |
| 33 | |
| 34 | UnwindPtrace::~UnwindPtrace() { |
| 35 | if (upt_info_) { |
| 36 | _UPT_destroy(upt_info_); |
| 37 | upt_info_ = NULL; |
| 38 | } |
| 39 | if (addr_space_) { |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame^] | 40 | // Remove the map from the address space before destroying it. |
| 41 | // It will be freed in the UnwindMap destructor. |
| 42 | unw_map_set(addr_space_, NULL); |
| 43 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 44 | unw_destroy_addr_space(addr_space_); |
| 45 | addr_space_ = NULL; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | bool UnwindPtrace::Unwind(size_t num_ignore_frames) { |
| 50 | addr_space_ = unw_create_addr_space(&_UPT_accessors, 0); |
| 51 | if (!addr_space_) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 52 | BACK_LOGW("unw_create_addr_space failed."); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame^] | 56 | UnwindMap* map = static_cast<UnwindMap*>(GetMap()); |
| 57 | unw_map_set(addr_space_, map->GetMapCursor()); |
| 58 | |
| 59 | upt_info_ = reinterpret_cast<struct UPT_info*>(_UPT_create(Tid())); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 60 | if (!upt_info_) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 61 | BACK_LOGW("Failed to create upt info."); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 65 | unw_cursor_t cursor; |
| 66 | int ret = unw_init_remote(&cursor, addr_space_, upt_info_); |
| 67 | if (ret < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 68 | BACK_LOGW("unw_init_remote failed %d", ret); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 72 | std::vector<backtrace_frame_data_t>* frames = GetFrames(); |
| 73 | frames->reserve(MAX_BACKTRACE_FRAMES); |
| 74 | size_t num_frames = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 75 | do { |
| 76 | unw_word_t pc; |
| 77 | ret = unw_get_reg(&cursor, UNW_REG_IP, &pc); |
| 78 | if (ret < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 79 | BACK_LOGW("Failed to read IP %d", ret); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 80 | break; |
| 81 | } |
| 82 | unw_word_t sp; |
| 83 | ret = unw_get_reg(&cursor, UNW_REG_SP, &sp); |
| 84 | if (ret < 0) { |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 85 | BACK_LOGW("Failed to read SP %d", ret); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 86 | break; |
| 87 | } |
| 88 | |
| 89 | if (num_ignore_frames == 0) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 90 | frames->resize(num_frames+1); |
| 91 | backtrace_frame_data_t* frame = &frames->at(num_frames); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 92 | frame->num = num_frames; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 93 | frame->pc = static_cast<uintptr_t>(pc); |
| 94 | frame->sp = static_cast<uintptr_t>(sp); |
| 95 | frame->stack_size = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 96 | |
| 97 | if (num_frames > 0) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 98 | backtrace_frame_data_t* prev = &frames->at(num_frames-1); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 99 | prev->stack_size = frame->sp - prev->sp; |
| 100 | } |
| 101 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame^] | 102 | frame->func_name = GetFunctionName(frame->pc, &frame->func_offset); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 103 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame^] | 104 | frame->map = FindMap(frame->pc); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 105 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 106 | num_frames++; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 107 | } else { |
| 108 | num_ignore_frames--; |
| 109 | } |
| 110 | ret = unw_step (&cursor); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 111 | } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | std::string UnwindPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) { |
| 117 | *offset = 0; |
| 118 | char buf[512]; |
| 119 | unw_word_t value; |
| 120 | if (unw_get_proc_name_by_ip(addr_space_, pc, buf, sizeof(buf), &value, |
| 121 | upt_info_) >= 0 && buf[0] != '\0') { |
| 122 | *offset = static_cast<uintptr_t>(value); |
| 123 | return buf; |
| 124 | } |
| 125 | return ""; |
| 126 | } |
| 127 | |
| 128 | //------------------------------------------------------------------------- |
| 129 | // C++ object creation function. |
| 130 | //------------------------------------------------------------------------- |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 131 | Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) { |
| 132 | return new BacktracePtrace(new UnwindPtrace(), pid, tid, map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 133 | } |