Chromium Code Reviews| 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 _Unwind_Reason_Code TraceStackFrame(_Unwind_Context* context, void* arg) { |
| 39 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg); | 29 StackCrawlState* state = static_cast<StackCrawlState*>(arg); |
| 40 | 30 |
| 31 // Clang's unwind.h doesn't provide _Unwind_GetIP on ARM, refer to | |
|
Mark Mentovai
2013/07/09 20:17:27
Tiny nit: comment ought to be indented +2.
scherkus (not reviewing)
2013/07/09 20:31:10
Done.
| |
| 32 // http://llvm.org/bugs/show_bug.cgi?id=16564 for details. | |
|
scherkus (not reviewing)
2013/07/08 20:37:27
FYI: filed bug upstream with llvm to figure out wh
| |
| 41 #if defined(__clang__) | 33 #if defined(__clang__) |
| 42 // Vanilla Clang's unwind.h doesn't have _Unwind_GetIP for ARM. | |
| 43 // See http://crbug.com/236855, too. | |
| 44 uintptr_t ip = 0; | 34 uintptr_t ip = 0; |
| 45 _Unwind_VRS_Get(context, _UVRSC_CORE, 15, _UVRSD_UINT32, &ip); | 35 _Unwind_VRS_Get(context, _UVRSC_CORE, 15, _UVRSD_UINT32, &ip); |
| 46 ip &= ~(uintptr_t)0x1; // remove thumb mode bit | 36 ip &= ~(uintptr_t)0x1; // Remove thumb mode bit. |
|
Mark Mentovai
2013/07/09 20:17:27
Maybe it’s better to refactor the comment and thes
scherkus (not reviewing)
2013/07/09 20:31:10
Ended up defining a local _Unwind_GetIP() guarded
| |
| 47 #else | 37 #else |
| 48 uintptr_t ip = _Unwind_GetIP(context); | 38 uintptr_t ip = _Unwind_GetIP(context); |
| 49 #endif | 39 #endif |
| 50 | 40 |
| 51 // The first stack frame is this function itself. Skip it. | 41 // The first stack frame is this function itself. Skip it. |
| 52 if (ip != 0 && !state->have_skipped_self) { | 42 if (ip != 0 && !state->have_skipped_self) { |
| 53 state->have_skipped_self = true; | 43 state->have_skipped_self = true; |
| 54 return _URC_NO_REASON; | 44 return _URC_NO_REASON; |
| 55 } | 45 } |
| 56 | 46 |
| 57 state->frames[state->frame_count++] = ip; | 47 state->frames[state->frame_count++] = ip; |
| 58 if (state->frame_count >= state->max_depth) | 48 if (state->frame_count >= state->max_depth) |
| 59 return _URC_END_OF_STACK; | 49 return _URC_END_OF_STACK; |
| 60 else | 50 return _URC_NO_REASON; |
| 61 return _URC_NO_REASON; | |
| 62 } | 51 } |
| 63 | 52 |
| 64 } // namespace | 53 } // namespace |
| 65 | 54 |
| 66 namespace base { | 55 namespace base { |
| 67 namespace debug { | 56 namespace debug { |
| 68 | 57 |
| 69 bool EnableInProcessStackDumping() { | 58 bool EnableInProcessStackDumping() { |
| 70 // When running in an application, our code typically expects SIGPIPE | 59 // When running in an application, our code typically expects SIGPIPE |
| 71 // to be ignored. Therefore, when testing that same code, it should run | 60 // to be ignored. Therefore, when testing that same code, it should run |
| 72 // with SIGPIPE ignored as well. | 61 // with SIGPIPE ignored as well. |
| 73 // TODO(phajdan.jr): De-duplicate this SIGPIPE code. | 62 // TODO(phajdan.jr): De-duplicate this SIGPIPE code. |
| 74 struct sigaction action; | 63 struct sigaction action; |
| 75 memset(&action, 0, sizeof(action)); | 64 memset(&action, 0, sizeof(action)); |
| 76 action.sa_handler = SIG_IGN; | 65 action.sa_handler = SIG_IGN; |
| 77 sigemptyset(&action.sa_mask); | 66 sigemptyset(&action.sa_mask); |
| 78 return (sigaction(SIGPIPE, &action, NULL) == 0); | 67 return (sigaction(SIGPIPE, &action, NULL) == 0); |
| 79 } | 68 } |
| 80 | 69 |
| 81 StackTrace::StackTrace() { | 70 StackTrace::StackTrace() { |
| 82 // TODO(dmikurube): Replace it with Bionic's get_backtrace(). | 71 StackCrawlState state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); |
| 83 // See http://crbug.com/236855. | 72 _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; | 73 count_ = state.frame_count; |
| 87 // TODO(dmikurube): Symbolize in Chrome. | |
| 88 } | 74 } |
| 89 | 75 |
| 90 void StackTrace::PrintBacktrace() const { | 76 void StackTrace::PrintBacktrace() const { |
| 91 std::string backtrace = ToString(); | 77 std::string backtrace = ToString(); |
| 92 __android_log_write(ANDROID_LOG_ERROR, "chromium", backtrace.c_str()); | 78 __android_log_write(ANDROID_LOG_ERROR, "chromium", backtrace.c_str()); |
| 93 } | 79 } |
| 94 | 80 |
| 95 // NOTE: Native libraries in APKs are stripped before installing. Print out the | 81 // 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 | 82 // relocatable address and library names so host computers can use tools to |
| 97 // symbolize and demangle (e.g., addr2line, c++filt). | 83 // symbolize and demangle (e.g., addr2line, c++filt). |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 } else { | 115 } else { |
| 130 *os << "<unknown>"; | 116 *os << "<unknown>"; |
| 131 } | 117 } |
| 132 | 118 |
| 133 *os << "\n"; | 119 *os << "\n"; |
| 134 } | 120 } |
| 135 } | 121 } |
| 136 | 122 |
| 137 } // namespace debug | 123 } // namespace debug |
| 138 } // namespace base | 124 } // namespace base |
| OLD | NEW |