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 #include <unwind.h> // TODO(dmikurube): Remove. See http://crbug.com/236855. |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/stringprintf.h" |
13 #include "base/third_party/symbolize/symbolize.h" | |
13 | 14 |
14 #ifdef __MIPSEL__ | 15 #ifdef __MIPSEL__ |
15 // SIGSTKFLT is not defined for MIPS. | 16 // SIGSTKFLT is not defined for MIPS. |
16 #define SIGSTKFLT SIGSEGV | 17 #define SIGSTKFLT SIGSEGV |
17 #endif | 18 #endif |
18 | 19 |
19 // TODO(dmikurube): Remove when Bionic's get_backtrace() gets popular. | 20 // TODO(dmikurube): Remove when Bionic's get_backtrace() gets popular. |
20 // See http://crbug.com/236855. | 21 // See http://crbug.com/236855. |
21 namespace { | 22 namespace { |
22 | 23 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 // See http://crbug.com/236855. | 90 // See http://crbug.com/236855. |
90 stack_crawl_state_t state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); | 91 stack_crawl_state_t state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); |
91 _Unwind_Backtrace(tracer, &state); | 92 _Unwind_Backtrace(tracer, &state); |
92 count_ = state.frame_count; | 93 count_ = state.frame_count; |
93 // TODO(dmikurube): Symbolize in Chrome. | 94 // TODO(dmikurube): Symbolize in Chrome. |
94 } | 95 } |
95 | 96 |
96 // Sends fake SIGSTKFLT signals to let the Android linker and debuggerd dump | 97 // Sends fake SIGSTKFLT signals to let the Android linker and debuggerd dump |
97 // stack. See inlined comments and Android bionic/linker/debugger.c and | 98 // stack. See inlined comments and Android bionic/linker/debugger.c and |
98 // system/core/debuggerd/debuggerd.c for details. | 99 // system/core/debuggerd/debuggerd.c for details. |
100 // | |
101 // TODO(scherkus): This should probably use OutputToStream() and print to | |
102 // stderr. Instead it crashes the program http://crbug.com/248775 | |
digit
2013/06/13 08:17:25
Small clarification here: outputting to stderr wil
| |
99 void StackTrace::PrintBacktrace() const { | 103 void StackTrace::PrintBacktrace() const { |
100 // Get the current handler of SIGSTKFLT for later use. | 104 // Get the current handler of SIGSTKFLT for later use. |
101 sighandler_t sig_handler = signal(SIGSTKFLT, SIG_DFL); | 105 sighandler_t sig_handler = signal(SIGSTKFLT, SIG_DFL); |
102 signal(SIGSTKFLT, sig_handler); | 106 signal(SIGSTKFLT, sig_handler); |
103 | 107 |
104 // The Android linker will handle this signal and send a stack dumping request | 108 // The Android linker will handle this signal and send a stack dumping request |
105 // to debuggerd which will ptrace_attach this process. Before returning from | 109 // to debuggerd which will ptrace_attach this process. Before returning from |
106 // the signal handler, the linker sets the signal handler to SIG_IGN. | 110 // the signal handler, the linker sets the signal handler to SIG_IGN. |
107 kill(gettid(), SIGSTKFLT); | 111 kill(gettid(), SIGSTKFLT); |
digit
2013/06/13 08:17:25
Note: I know you didn't modify that part, but I'm
scherkus (not reviewing)
2013/06/13 17:35:05
You know what? I'm going to rip out this code and
| |
108 | 112 |
109 // Because debuggerd will wait for the process to be stopped by the actual | 113 // Because debuggerd will wait for the process to be stopped by the actual |
110 // signal in crashing scenarios, signal is sent again to met the expectation. | 114 // signal in crashing scenarios, signal is sent again to met the expectation. |
111 // Debuggerd will dump stack into the system log and /data/tombstones/ files. | 115 // Debuggerd will dump stack into the system log and /data/tombstones/ files. |
112 // NOTE: If this process runs in the interactive shell, it will be put | 116 // NOTE: If this process runs in the interactive shell, it will be put |
113 // in the background. To resume it in the foreground, use 'fg' command. | 117 // in the background. To resume it in the foreground, use 'fg' command. |
114 kill(gettid(), SIGSTKFLT); | 118 kill(gettid(), SIGSTKFLT); |
115 | 119 |
116 // Restore the signal handler so that this method can work the next time. | 120 // Restore the signal handler so that this method can work the next time. |
117 signal(SIGSTKFLT, sig_handler); | 121 signal(SIGSTKFLT, sig_handler); |
digit
2013/06/13 08:17:25
Note: Oops, we should never use signal() to save/r
| |
118 } | 122 } |
119 | 123 |
124 // NOTE: Native libraries in APKs are stripped before installing. Print out the | |
125 // relocatable address and library names so host computers can use tools to | |
126 // symbolize and demangle (e.g., addr2line, c++filt). | |
120 void StackTrace::OutputToStream(std::ostream* os) const { | 127 void StackTrace::OutputToStream(std::ostream* os) const { |
121 NOTIMPLEMENTED(); | 128 for (size_t i = 0; i < count_; ++i) { |
129 *os << base::StringPrintf("#%02d ", i); | |
130 | |
131 // Subtract by one as return address of function may be in the next | |
132 // function when a function is annotated as noreturn. | |
133 void* address = static_cast<char*>(trace_[i]) - 1; | |
134 | |
135 char path[1024]; | |
136 uint64 start_address = 0; | |
137 if (google::FindObjectFileNameContainingPcAndGetStartAddress( | |
138 address, path, sizeof(path), &start_address)) { | |
139 uintptr_t rel_pc = reinterpret_cast<uintptr_t>(address) - start_address; | |
140 *os << base::StringPrintf("pc %08x %s", rel_pc, path); | |
satorux1
2013/06/13 03:34:25
http://google-styleguide.googlecode.com/svn/trunk/
digit
2013/06/13 08:17:25
nit: That would be very wrong, uintptr_t is always
scherkus (not reviewing)
2013/06/13 17:35:05
Open to ideas ... I tried using PRIxPTR but got si
satorux1
2013/06/14 01:45:41
Ah, I misunderstood that we were handling uint64 h
| |
141 } else { | |
142 *os << "<unknown>"; | |
digit
2013/06/13 08:17:25
I'd suggest printing the address value as well, ju
scherkus (not reviewing)
2013/06/13 17:35:05
Done.
| |
143 } | |
144 | |
145 *os << "\n"; | |
146 } | |
122 } | 147 } |
123 | 148 |
124 } // namespace debug | 149 } // namespace debug |
125 } // namespace base | 150 } // namespace base |
OLD | NEW |