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 when get_backtrace() is used. | |
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 Bionic's libc/bionic/debug_stacktrace.cpp. | |
digit
2013/04/30 16:58:13
Maybe file a bug to track this and put a reference
Dai Mikurube (NOT FULLTIME)
2013/04/30 17:15:23
Done.
| |
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 uintptr_t ip = _Unwind_GetIP(context); | |
48 | |
49 // The first stack frame is get_backtrace itself. Skip it. | |
digit
2013/04/30 16:58:13
I'd suggest replacing "get_backtrace" by "this fun
Dai Mikurube (NOT FULLTIME)
2013/04/30 17:15:23
Done.
| |
50 if (ip != 0 && !state->have_skipped_self) { | |
51 state->have_skipped_self = true; | |
52 return _URC_NO_REASON; | |
53 } | |
54 | |
55 state->frames[state->frame_count++] = ip; | |
56 if (state->frame_count >= state->max_depth) | |
57 return _URC_END_OF_STACK; | |
58 else | |
59 return _URC_NO_REASON; | |
60 } | |
61 | |
62 } | |
63 | |
18 namespace base { | 64 namespace base { |
19 namespace debug { | 65 namespace debug { |
20 | 66 |
21 bool EnableInProcessStackDumping() { | 67 bool EnableInProcessStackDumping() { |
22 // When running in an application, our code typically expects SIGPIPE | 68 // When running in an application, our code typically expects SIGPIPE |
23 // to be ignored. Therefore, when testing that same code, it should run | 69 // to be ignored. Therefore, when testing that same code, it should run |
24 // with SIGPIPE ignored as well. | 70 // with SIGPIPE ignored as well. |
25 // TODO(phajdan.jr): De-duplicate this SIGPIPE code. | 71 // TODO(phajdan.jr): De-duplicate this SIGPIPE code. |
26 struct sigaction action; | 72 struct sigaction action; |
27 memset(&action, 0, sizeof(action)); | 73 memset(&action, 0, sizeof(action)); |
28 action.sa_handler = SIG_IGN; | 74 action.sa_handler = SIG_IGN; |
29 sigemptyset(&action.sa_mask); | 75 sigemptyset(&action.sa_mask); |
30 return (sigaction(SIGPIPE, &action, NULL) == 0); | 76 return (sigaction(SIGPIPE, &action, NULL) == 0); |
31 } | 77 } |
32 | 78 |
33 StackTrace::StackTrace() { | 79 StackTrace::StackTrace() { |
80 // TODO(dmikurube): Replace it with Bionic's get_backtrace(). | |
81 // See Bionic's libc/bionic/debug_stacktrace.cpp. | |
82 stack_crawl_state_t state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); | |
83 _Unwind_Backtrace(tracer, &state); | |
84 count_ = state.frame_count; | |
85 // TODO(dmikurube): Symbolize in Chrome. | |
34 } | 86 } |
35 | 87 |
36 // Sends fake SIGSTKFLT signals to let the Android linker and debuggerd dump | 88 // Sends fake SIGSTKFLT signals to let the Android linker and debuggerd dump |
37 // stack. See inlined comments and Android bionic/linker/debugger.c and | 89 // stack. See inlined comments and Android bionic/linker/debugger.c and |
38 // system/core/debuggerd/debuggerd.c for details. | 90 // system/core/debuggerd/debuggerd.c for details. |
39 void StackTrace::PrintBacktrace() const { | 91 void StackTrace::PrintBacktrace() const { |
40 // Get the current handler of SIGSTKFLT for later use. | 92 // Get the current handler of SIGSTKFLT for later use. |
41 sighandler_t sig_handler = signal(SIGSTKFLT, SIG_DFL); | 93 sighandler_t sig_handler = signal(SIGSTKFLT, SIG_DFL); |
42 signal(SIGSTKFLT, sig_handler); | 94 signal(SIGSTKFLT, sig_handler); |
43 | 95 |
(...skipping 12 matching lines...) Expand all Loading... | |
56 // Restore the signal handler so that this method can work the next time. | 108 // Restore the signal handler so that this method can work the next time. |
57 signal(SIGSTKFLT, sig_handler); | 109 signal(SIGSTKFLT, sig_handler); |
58 } | 110 } |
59 | 111 |
60 void StackTrace::OutputToStream(std::ostream* os) const { | 112 void StackTrace::OutputToStream(std::ostream* os) const { |
61 NOTIMPLEMENTED(); | 113 NOTIMPLEMENTED(); |
62 } | 114 } |
63 | 115 |
64 } // namespace debug | 116 } // namespace debug |
65 } // namespace base | 117 } // namespace base |
OLD | NEW |