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 #include <iomanip> | |
11 | 12 |
12 #include "base/logging.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 | |
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); |
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); |
118 } | 122 } |
119 | 123 |
120 void StackTrace::OutputToStream(std::ostream* os) const { | 124 void StackTrace::OutputToStream(std::ostream* os) const { |
121 NOTIMPLEMENTED(); | 125 std::ios_base::fmtflags flags = os->flags(); |
126 | |
127 for (size_t i = 0; i < count_; ++i) { | |
128 // NOTE: Native libraries in APKs are stripped before installing. Print out | |
129 // the relocatable address and library names so host computers can use tools | |
130 // to symbolize and demangle (e.g., addr2line, c++filt). | |
131 char path[1024]; | |
132 uint64 start_address; | |
satorux1
2013/06/13 01:58:10
Please move them to where these are used.
I'd als
scherkus (not reviewing)
2013/06/13 02:20:46
Done.
| |
133 | |
134 *os << "#" << std::dec << std::setfill('0') << std::setw(2) << i << " "; | |
satorux1
2013/06/13 01:58:10
For formatting, I think base::StringPrintf() is mo
scherkus (not reviewing)
2013/06/13 02:20:46
Yeah -- part of me wanted to make things async saf
satorux1
2013/06/13 03:34:25
For now, maybe add a TODO comment?
| |
135 | |
136 // Subtract by one as return address of function may be in the next | |
137 // function when a function is annotated as noreturn. | |
138 void* address = static_cast<char*>(trace_[i]) - 1; | |
139 if (google::FindObjectFileNameContainingPcAndGetStartAddress( | |
140 address, path, sizeof(path), &start_address)) { | |
141 uintptr_t rel_pc = reinterpret_cast<uintptr_t>(address) - start_address; | |
142 *os << "pc " << std::hex << std::setfill('0') << std::setw(8) << rel_pc | |
143 << " " << path; | |
144 } else { | |
145 *os << "<unknown>"; | |
146 } | |
147 | |
148 *os << "\n"; | |
149 } | |
150 | |
151 os->flags(flags); | |
122 } | 152 } |
123 | 153 |
124 } // namespace debug | 154 } // namespace debug |
125 } // namespace base | 155 } // namespace base |
OLD | NEW |