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 <android/log.h> |
8 #include <sys/types.h> | |
9 #include <unistd.h> | |
10 #include <unwind.h> // TODO(dmikurube): Remove. See http://crbug.com/236855. | 8 #include <unwind.h> // TODO(dmikurube): Remove. See http://crbug.com/236855. |
11 | 9 |
12 #include "base/logging.h" | 10 #include "base/debug/proc_maps_linux.h" |
13 | 11 #include "base/strings/stringprintf.h" |
14 #ifdef __MIPSEL__ | |
15 // SIGSTKFLT is not defined for MIPS. | |
16 #define SIGSTKFLT SIGSEGV | |
17 #endif | |
18 | 12 |
19 // TODO(dmikurube): Remove when Bionic's get_backtrace() gets popular. | 13 // TODO(dmikurube): Remove when Bionic's get_backtrace() gets popular. |
20 // See http://crbug.com/236855. | 14 // See http://crbug.com/236855. |
21 namespace { | 15 namespace { |
22 | 16 |
23 /* depends how the system includes define this */ | 17 /* depends how the system includes define this */ |
24 #ifdef HAVE_UNWIND_CONTEXT_STRUCT | 18 #ifdef HAVE_UNWIND_CONTEXT_STRUCT |
25 typedef struct _Unwind_Context __unwind_context; | 19 typedef struct _Unwind_Context __unwind_context; |
26 #else | 20 #else |
27 typedef _Unwind_Context __unwind_context; | 21 typedef _Unwind_Context __unwind_context; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 | 80 |
87 StackTrace::StackTrace() { | 81 StackTrace::StackTrace() { |
88 // TODO(dmikurube): Replace it with Bionic's get_backtrace(). | 82 // TODO(dmikurube): Replace it with Bionic's get_backtrace(). |
89 // See http://crbug.com/236855. | 83 // See http://crbug.com/236855. |
90 stack_crawl_state_t state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); | 84 stack_crawl_state_t state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); |
91 _Unwind_Backtrace(tracer, &state); | 85 _Unwind_Backtrace(tracer, &state); |
92 count_ = state.frame_count; | 86 count_ = state.frame_count; |
93 // TODO(dmikurube): Symbolize in Chrome. | 87 // TODO(dmikurube): Symbolize in Chrome. |
94 } | 88 } |
95 | 89 |
96 // 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 // system/core/debuggerd/debuggerd.c for details. | |
99 void StackTrace::PrintBacktrace() const { | 90 void StackTrace::PrintBacktrace() const { |
100 // Get the current handler of SIGSTKFLT for later use. | 91 std::string backtrace = ToString(); |
101 sighandler_t sig_handler = signal(SIGSTKFLT, SIG_DFL); | 92 __android_log_write(ANDROID_LOG_ERROR, "chromium", backtrace.c_str()); |
102 signal(SIGSTKFLT, sig_handler); | |
103 | |
104 // 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 | |
106 // the signal handler, the linker sets the signal handler to SIG_IGN. | |
107 kill(gettid(), SIGSTKFLT); | |
108 | |
109 // 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. | |
111 // 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 | |
113 // in the background. To resume it in the foreground, use 'fg' command. | |
114 kill(gettid(), SIGSTKFLT); | |
115 | |
116 // Restore the signal handler so that this method can work the next time. | |
117 signal(SIGSTKFLT, sig_handler); | |
118 } | 93 } |
119 | 94 |
95 // 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 | |
97 // symbolize and demangle (e.g., addr2line, c++filt). | |
120 void StackTrace::OutputToStream(std::ostream* os) const { | 98 void StackTrace::OutputToStream(std::ostream* os) const { |
121 NOTIMPLEMENTED(); | 99 std::string proc_maps; |
100 std::vector<MappedMemoryRegion> regions; | |
101 if (!ReadProcMaps(&proc_maps)) { | |
102 __android_log_write( | |
103 ANDROID_LOG_ERROR, "chromium", "Failed to read /proc/self/maps"); | |
104 } else if (!ParseProcMaps(proc_maps, ®ions)) { | |
105 __android_log_write( | |
106 ANDROID_LOG_ERROR, "chromium", "Failed to parse /proc/self/maps"); | |
107 } | |
108 | |
109 for (size_t i = 0; i < count_; ++i) { | |
110 *os << base::StringPrintf("#%02d ", i); | |
111 | |
112 // Subtract by one as return address of function may be in the next | |
Mark Mentovai
2013/07/03 14:03:30
Nit: “subtract one,” not “subtract by one.”
scherkus (not reviewing)
2013/07/03 19:43:01
Done.
| |
113 // function when a function is annotated as noreturn. | |
Mark Mentovai
2013/07/03 14:03:30
This comment only tells half the story. If someone
scherkus (not reviewing)
2013/07/03 19:43:01
I fully admit that this is vestigial code from whe
| |
114 uintptr_t address = reinterpret_cast<uintptr_t>(trace_[i]) - 1; | |
115 | |
116 std::vector<MappedMemoryRegion>::iterator iter = regions.begin(); | |
117 while (iter != regions.end()) { | |
118 if (address >= iter->start && address < iter->end && | |
119 (iter->permissions & MappedMemoryRegion::READ) && | |
Mark Mentovai
2013/07/03 14:03:30
Why check the protection bits? If the address is i
scherkus (not reviewing)
2013/07/03 19:43:01
do you mean non-zero inode/device?
considering we
Mark Mentovai
2013/07/04 01:25:49
scherkus wrote:
| |
120 (iter->permissions & MappedMemoryRegion::EXECUTE)) { | |
121 break; | |
122 } | |
123 ++iter; | |
124 } | |
125 | |
126 if (iter != regions.end()) { | |
127 uintptr_t rel_pc = address - iter->start + iter->offset; | |
128 const char* path = iter->path.c_str(); | |
129 *os << base::StringPrintf("pc %08x %s", rel_pc, path); | |
Mark Mentovai
2013/07/03 14:03:30
Maybe it’s just me, but this format seems kind of
scherkus (not reviewing)
2013/07/03 19:43:01
Was attempting to emulate the Android stack dump s
| |
130 } else { | |
131 *os << base::StringPrintf("<unknown> 0x%08x", address); | |
Mark Mentovai
2013/07/03 14:03:30
kinda like the format you used here.
But why’d yo
scherkus (not reviewing)
2013/07/03 19:43:01
Done.
| |
132 } | |
133 | |
134 *os << "\n"; | |
135 } | |
122 } | 136 } |
123 | 137 |
124 } // namespace debug | 138 } // namespace debug |
125 } // namespace base | 139 } // namespace base |
OLD | NEW |