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 <signal.h> | 7 #include <signal.h> |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 #include <unwind.h> // TODO(dmikurube): Remove. See http://crbug.com/236855. |
10 | 11 |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 | 13 |
13 #ifdef __MIPSEL__ | 14 #ifdef __MIPSEL__ |
14 // SIGSTKFLT is not defined for MIPS. | 15 // SIGSTKFLT is not defined for MIPS. |
15 #define SIGSTKFLT SIGSEGV | 16 #define SIGSTKFLT SIGSEGV |
16 #endif | 17 #endif |
17 | 18 |
| 19 // TODO(dmikurube): Remove when Bionic's get_backtrace() gets popular. |
| 20 // See http://crbug.com/236855. |
| 21 namespace { |
| 22 |
| 23 /* depends how the system includes define this */ |
| 24 #ifdef HAVE_UNWIND_CONTEXT_STRUCT |
| 25 typedef struct _Unwind_Context __unwind_context; |
| 26 #else |
| 27 typedef _Unwind_Context __unwind_context; |
| 28 #endif |
| 29 |
| 30 struct stack_crawl_state_t { |
| 31 uintptr_t* frames; |
| 32 size_t frame_count; |
| 33 size_t max_depth; |
| 34 bool have_skipped_self; |
| 35 |
| 36 stack_crawl_state_t(uintptr_t* frames, size_t max_depth) |
| 37 : frames(frames), |
| 38 frame_count(0), |
| 39 max_depth(max_depth), |
| 40 have_skipped_self(false) { |
| 41 } |
| 42 }; |
| 43 |
| 44 static _Unwind_Reason_Code tracer(__unwind_context* context, void* arg) { |
| 45 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg); |
| 46 |
| 47 #if defined(__clang__) |
| 48 // Vanilla Clang's unwind.h doesn't have _Unwind_GetIP for ARM. |
| 49 // See http://crbug.com/236855, too. |
| 50 uintptr_t ip = 0; |
| 51 _Unwind_VRS_Get(context, _UVRSC_CORE, 15, _UVRSD_UINT32, &ip); |
| 52 ip &= ~(uintptr_t)0x1; // remove thumb mode bit |
| 53 #else |
| 54 uintptr_t ip = _Unwind_GetIP(context); |
| 55 #endif |
| 56 |
| 57 // The first stack frame is this function itself. Skip it. |
| 58 if (ip != 0 && !state->have_skipped_self) { |
| 59 state->have_skipped_self = true; |
| 60 return _URC_NO_REASON; |
| 61 } |
| 62 |
| 63 state->frames[state->frame_count++] = ip; |
| 64 if (state->frame_count >= state->max_depth) |
| 65 return _URC_END_OF_STACK; |
| 66 else |
| 67 return _URC_NO_REASON; |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 |
18 namespace base { | 72 namespace base { |
19 namespace debug { | 73 namespace debug { |
20 | 74 |
21 bool EnableInProcessStackDumping() { | 75 bool EnableInProcessStackDumping() { |
22 // When running in an application, our code typically expects SIGPIPE | 76 // When running in an application, our code typically expects SIGPIPE |
23 // to be ignored. Therefore, when testing that same code, it should run | 77 // to be ignored. Therefore, when testing that same code, it should run |
24 // with SIGPIPE ignored as well. | 78 // with SIGPIPE ignored as well. |
25 // TODO(phajdan.jr): De-duplicate this SIGPIPE code. | 79 // TODO(phajdan.jr): De-duplicate this SIGPIPE code. |
26 struct sigaction action; | 80 struct sigaction action; |
27 memset(&action, 0, sizeof(action)); | 81 memset(&action, 0, sizeof(action)); |
28 action.sa_handler = SIG_IGN; | 82 action.sa_handler = SIG_IGN; |
29 sigemptyset(&action.sa_mask); | 83 sigemptyset(&action.sa_mask); |
30 return (sigaction(SIGPIPE, &action, NULL) == 0); | 84 return (sigaction(SIGPIPE, &action, NULL) == 0); |
31 } | 85 } |
32 | 86 |
33 StackTrace::StackTrace() { | 87 StackTrace::StackTrace() { |
| 88 // TODO(dmikurube): Replace it with Bionic's get_backtrace(). |
| 89 // See http://crbug.com/236855. |
| 90 stack_crawl_state_t state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); |
| 91 _Unwind_Backtrace(tracer, &state); |
| 92 count_ = state.frame_count; |
| 93 // TODO(dmikurube): Symbolize in Chrome. |
34 } | 94 } |
35 | 95 |
36 // Sends fake SIGSTKFLT signals to let the Android linker and debuggerd dump | 96 // Sends fake SIGSTKFLT signals to let the Android linker and debuggerd dump |
37 // stack. See inlined comments and Android bionic/linker/debugger.c and | 97 // stack. See inlined comments and Android bionic/linker/debugger.c and |
38 // system/core/debuggerd/debuggerd.c for details. | 98 // system/core/debuggerd/debuggerd.c for details. |
39 void StackTrace::PrintBacktrace() const { | 99 void StackTrace::PrintBacktrace() const { |
40 // Get the current handler of SIGSTKFLT for later use. | 100 // Get the current handler of SIGSTKFLT for later use. |
41 sighandler_t sig_handler = signal(SIGSTKFLT, SIG_DFL); | 101 sighandler_t sig_handler = signal(SIGSTKFLT, SIG_DFL); |
42 signal(SIGSTKFLT, sig_handler); | 102 signal(SIGSTKFLT, sig_handler); |
43 | 103 |
(...skipping 12 matching lines...) Expand all Loading... |
56 // Restore the signal handler so that this method can work the next time. | 116 // Restore the signal handler so that this method can work the next time. |
57 signal(SIGSTKFLT, sig_handler); | 117 signal(SIGSTKFLT, sig_handler); |
58 } | 118 } |
59 | 119 |
60 void StackTrace::OutputToStream(std::ostream* os) const { | 120 void StackTrace::OutputToStream(std::ostream* os) const { |
61 NOTIMPLEMENTED(); | 121 NOTIMPLEMENTED(); |
62 } | 122 } |
63 | 123 |
64 } // namespace debug | 124 } // namespace debug |
65 } // namespace base | 125 } // namespace base |
OLD | NEW |