| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/debug/stack_trace.h" | 5 #include "base/debug/stack_trace.h" |
| 6 | 6 |
| 7 #include <android/log.h> | 7 #include <android/log.h> |
| 8 #include <unwind.h> // TODO(dmikurube): Remove. See http://crbug.com/236855. | 8 #include <unwind.h> |
| 9 | 9 |
| 10 #include "base/debug/proc_maps_linux.h" | 10 #include "base/debug/proc_maps_linux.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 | 12 |
| 13 // TODO(dmikurube): Remove when Bionic's get_backtrace() gets popular. | |
| 14 // See http://crbug.com/236855. | |
| 15 namespace { | 13 namespace { |
| 16 | 14 |
| 17 /* depends how the system includes define this */ | 15 struct StackCrawlState { |
| 18 #ifdef HAVE_UNWIND_CONTEXT_STRUCT | 16 StackCrawlState(uintptr_t* frames, size_t max_depth) |
| 19 typedef struct _Unwind_Context __unwind_context; | 17 : frames(frames), |
| 20 #else | 18 frame_count(0), |
| 21 typedef _Unwind_Context __unwind_context; | 19 max_depth(max_depth), |
| 22 #endif | 20 have_skipped_self(false) {} |
| 23 | 21 |
| 24 struct stack_crawl_state_t { | |
| 25 uintptr_t* frames; | 22 uintptr_t* frames; |
| 26 size_t frame_count; | 23 size_t frame_count; |
| 27 size_t max_depth; | 24 size_t max_depth; |
| 28 bool have_skipped_self; | 25 bool have_skipped_self; |
| 29 | |
| 30 stack_crawl_state_t(uintptr_t* frames, size_t max_depth) | |
| 31 : frames(frames), | |
| 32 frame_count(0), | |
| 33 max_depth(max_depth), | |
| 34 have_skipped_self(false) { | |
| 35 } | |
| 36 }; | 26 }; |
| 37 | 27 |
| 38 static _Unwind_Reason_Code tracer(__unwind_context* context, void* arg) { | 28 // Clang's unwind.h doesn't provide _Unwind_GetIP on ARM, refer to |
| 39 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg); | 29 // http://llvm.org/bugs/show_bug.cgi?id=16564 for details. |
| 40 | |
| 41 #if defined(__clang__) | 30 #if defined(__clang__) |
| 42 // Vanilla Clang's unwind.h doesn't have _Unwind_GetIP for ARM. | 31 uintptr_t _Unwind_GetIP(_Unwind_Context* context) { |
| 43 // See http://crbug.com/236855, too. | |
| 44 uintptr_t ip = 0; | 32 uintptr_t ip = 0; |
| 45 _Unwind_VRS_Get(context, _UVRSC_CORE, 15, _UVRSD_UINT32, &ip); | 33 _Unwind_VRS_Get(context, _UVRSC_CORE, 15, _UVRSD_UINT32, &ip); |
| 46 ip &= ~(uintptr_t)0x1; // remove thumb mode bit | 34 return ip & ~static_cast<uintptr_t>(0x1); // Remove thumb mode bit. |
| 47 #else | 35 } |
| 36 #endif |
| 37 |
| 38 _Unwind_Reason_Code TraceStackFrame(_Unwind_Context* context, void* arg) { |
| 39 StackCrawlState* state = static_cast<StackCrawlState*>(arg); |
| 48 uintptr_t ip = _Unwind_GetIP(context); | 40 uintptr_t ip = _Unwind_GetIP(context); |
| 49 #endif | |
| 50 | 41 |
| 51 // The first stack frame is this function itself. Skip it. | 42 // The first stack frame is this function itself. Skip it. |
| 52 if (ip != 0 && !state->have_skipped_self) { | 43 if (ip != 0 && !state->have_skipped_self) { |
| 53 state->have_skipped_self = true; | 44 state->have_skipped_self = true; |
| 54 return _URC_NO_REASON; | 45 return _URC_NO_REASON; |
| 55 } | 46 } |
| 56 | 47 |
| 57 state->frames[state->frame_count++] = ip; | 48 state->frames[state->frame_count++] = ip; |
| 58 if (state->frame_count >= state->max_depth) | 49 if (state->frame_count >= state->max_depth) |
| 59 return _URC_END_OF_STACK; | 50 return _URC_END_OF_STACK; |
| 60 else | 51 return _URC_NO_REASON; |
| 61 return _URC_NO_REASON; | |
| 62 } | 52 } |
| 63 | 53 |
| 64 } // namespace | 54 } // namespace |
| 65 | 55 |
| 66 namespace base { | 56 namespace base { |
| 67 namespace debug { | 57 namespace debug { |
| 68 | 58 |
| 69 bool EnableInProcessStackDumping() { | 59 bool EnableInProcessStackDumping() { |
| 70 // When running in an application, our code typically expects SIGPIPE | 60 // When running in an application, our code typically expects SIGPIPE |
| 71 // to be ignored. Therefore, when testing that same code, it should run | 61 // to be ignored. Therefore, when testing that same code, it should run |
| 72 // with SIGPIPE ignored as well. | 62 // with SIGPIPE ignored as well. |
| 73 // TODO(phajdan.jr): De-duplicate this SIGPIPE code. | 63 // TODO(phajdan.jr): De-duplicate this SIGPIPE code. |
| 74 struct sigaction action; | 64 struct sigaction action; |
| 75 memset(&action, 0, sizeof(action)); | 65 memset(&action, 0, sizeof(action)); |
| 76 action.sa_handler = SIG_IGN; | 66 action.sa_handler = SIG_IGN; |
| 77 sigemptyset(&action.sa_mask); | 67 sigemptyset(&action.sa_mask); |
| 78 return (sigaction(SIGPIPE, &action, NULL) == 0); | 68 return (sigaction(SIGPIPE, &action, NULL) == 0); |
| 79 } | 69 } |
| 80 | 70 |
| 81 StackTrace::StackTrace() { | 71 StackTrace::StackTrace() { |
| 82 // TODO(dmikurube): Replace it with Bionic's get_backtrace(). | 72 StackCrawlState state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); |
| 83 // See http://crbug.com/236855. | 73 _Unwind_Backtrace(&TraceStackFrame, &state); |
| 84 stack_crawl_state_t state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); | |
| 85 _Unwind_Backtrace(tracer, &state); | |
| 86 count_ = state.frame_count; | 74 count_ = state.frame_count; |
| 87 // TODO(dmikurube): Symbolize in Chrome. | |
| 88 } | 75 } |
| 89 | 76 |
| 90 void StackTrace::PrintBacktrace() const { | 77 void StackTrace::PrintBacktrace() const { |
| 91 std::string backtrace = ToString(); | 78 std::string backtrace = ToString(); |
| 92 __android_log_write(ANDROID_LOG_ERROR, "chromium", backtrace.c_str()); | 79 __android_log_write(ANDROID_LOG_ERROR, "chromium", backtrace.c_str()); |
| 93 } | 80 } |
| 94 | 81 |
| 95 // NOTE: Native libraries in APKs are stripped before installing. Print out the | 82 // NOTE: Native libraries in APKs are stripped before installing. Print out the |
| 96 // relocatable address and library names so host computers can use tools to | 83 // relocatable address and library names so host computers can use tools to |
| 97 // symbolize and demangle (e.g., addr2line, c++filt). | 84 // symbolize and demangle (e.g., addr2line, c++filt). |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 } else { | 116 } else { |
| 130 *os << "<unknown>"; | 117 *os << "<unknown>"; |
| 131 } | 118 } |
| 132 | 119 |
| 133 *os << "\n"; | 120 *os << "\n"; |
| 134 } | 121 } |
| 135 } | 122 } |
| 136 | 123 |
| 137 } // namespace debug | 124 } // namespace debug |
| 138 } // namespace base | 125 } // namespace base |
| OLD | NEW |