Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: base/debug/stack_trace_android.cc

Issue 16770006: Implement basic stack traces on Android and reenable unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixes Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base.gyp ('k') | base/logging.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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, &regions)) {
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 // Subtract one as return address of function may be in the next
111 // function when a function is annotated as noreturn.
112 uintptr_t address = reinterpret_cast<uintptr_t>(trace_[i]) - 1;
Mark Mentovai 2013/07/04 01:25:49 OK, I guess the safe thing to do is leave this as
113
114 std::vector<MappedMemoryRegion>::iterator iter = regions.begin();
115 while (iter != regions.end()) {
116 if (address >= iter->start && address < iter->end &&
117 !iter->path.empty()) {
118 break;
119 }
120 ++iter;
121 }
122
123 *os << base::StringPrintf("#%02d 0x%08x ", i, address);
Mark Mentovai 2013/07/04 01:25:49 This format is awesome.
124
125 if (iter != regions.end()) {
126 uintptr_t rel_pc = address - iter->start + iter->offset;
127 const char* path = iter->path.c_str();
128 *os << base::StringPrintf("%s+0x%08x", path, rel_pc);
129 } else {
130 *os << "<unknown>";
131 }
132
133 *os << "\n";
134 }
122 } 135 }
123 136
124 } // namespace debug 137 } // namespace debug
125 } // namespace base 138 } // namespace base
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | base/logging.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698