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

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: Created 7 years, 6 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/debug/stack_trace_unittest.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 <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/demangle.h"
14 #include "base/third_party/symbolize/symbolize.h"
13 15
14 #ifdef __MIPSEL__ 16 #ifdef __MIPSEL__
15 // SIGSTKFLT is not defined for MIPS. 17 // SIGSTKFLT is not defined for MIPS.
16 #define SIGSTKFLT SIGSEGV 18 #define SIGSTKFLT SIGSEGV
17 #endif 19 #endif
18 20
19 // TODO(dmikurube): Remove when Bionic's get_backtrace() gets popular. 21 // TODO(dmikurube): Remove when Bionic's get_backtrace() gets popular.
20 // See http://crbug.com/236855. 22 // See http://crbug.com/236855.
21 namespace { 23 namespace {
22 24
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 // See http://crbug.com/236855. 91 // See http://crbug.com/236855.
90 stack_crawl_state_t state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); 92 stack_crawl_state_t state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces);
91 _Unwind_Backtrace(tracer, &state); 93 _Unwind_Backtrace(tracer, &state);
92 count_ = state.frame_count; 94 count_ = state.frame_count;
93 // TODO(dmikurube): Symbolize in Chrome. 95 // TODO(dmikurube): Symbolize in Chrome.
94 } 96 }
95 97
96 // Sends fake SIGSTKFLT signals to let the Android linker and debuggerd dump 98 // Sends fake SIGSTKFLT signals to let the Android linker and debuggerd dump
97 // stack. See inlined comments and Android bionic/linker/debugger.c and 99 // stack. See inlined comments and Android bionic/linker/debugger.c and
98 // system/core/debuggerd/debuggerd.c for details. 100 // system/core/debuggerd/debuggerd.c for details.
99 void StackTrace::PrintBacktrace() const { 101 void StackTrace::PrintBacktrace() const {
satorux1 2013/06/12 05:43:42 PrintBacktrace() and OutputToStream() will behave
scherkus (not reviewing) 2013/06/13 01:27:44 I noticed this too -- I filed a bug and am trying
100 // Get the current handler of SIGSTKFLT for later use. 102 // Get the current handler of SIGSTKFLT for later use.
101 sighandler_t sig_handler = signal(SIGSTKFLT, SIG_DFL); 103 sighandler_t sig_handler = signal(SIGSTKFLT, SIG_DFL);
102 signal(SIGSTKFLT, sig_handler); 104 signal(SIGSTKFLT, sig_handler);
103 105
104 // The Android linker will handle this signal and send a stack dumping request 106 // 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 107 // to debuggerd which will ptrace_attach this process. Before returning from
106 // the signal handler, the linker sets the signal handler to SIG_IGN. 108 // the signal handler, the linker sets the signal handler to SIG_IGN.
107 kill(gettid(), SIGSTKFLT); 109 kill(gettid(), SIGSTKFLT);
108 110
109 // Because debuggerd will wait for the process to be stopped by the actual 111 // 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. 112 // 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. 113 // 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 114 // 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. 115 // in the background. To resume it in the foreground, use 'fg' command.
114 kill(gettid(), SIGSTKFLT); 116 kill(gettid(), SIGSTKFLT);
115 117
116 // Restore the signal handler so that this method can work the next time. 118 // Restore the signal handler so that this method can work the next time.
117 signal(SIGSTKFLT, sig_handler); 119 signal(SIGSTKFLT, sig_handler);
118 } 120 }
119 121
122 static int OnSymbolizeCallback(int fd,
123 void* pc,
124 char* out,
125 size_t out_size,
126 uint64 relocation) {
127 // Maximum string length of an int is 10 characters.
128 char path[32];
129 int size = snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
130 if (size < 0 || static_cast<size_t>(size) >= sizeof(path))
131 return -1;
132
133 char buf[1024];
134 size = readlink(path, buf, sizeof(buf));
scherkus (not reviewing) 2013/06/12 00:43:43 satorux: this is also kind of unnecessary since sy
satorux1 2013/06/12 05:43:42 I think it's easy to change symbolize.cc to provid
135 if (size < 0 || static_cast<size_t>(size) >= sizeof(buf))
136 return -1;
137 buf[size] = '\0'; // readlink() doesn't null-terminate strings.
138
139 uintptr_t rel_pc = reinterpret_cast<uintptr_t>(pc) - relocation;
140 size = snprintf(out, out_size, "pc %08x %s ", rel_pc, buf);
141 return size;
142 }
143
120 void StackTrace::OutputToStream(std::ostream* os) const { 144 void StackTrace::OutputToStream(std::ostream* os) const {
121 NOTIMPLEMENTED(); 145 // TODO(scherkus): HACK - is this thread/async safe and/or proper usage?
146 google::InstallSymbolizeCallback(&OnSymbolizeCallback);
scherkus (not reviewing) 2013/06/12 00:43:43 satorux: is this intended usage? I couldn't find a
satorux1 2013/06/12 05:43:42 IIRC, this awkward callback hook was introduced to
147
148 for (size_t i = 0; i < count_; ++i) {
149 // NOTE: Native libraries in APKs are stripped before installing. Print out
150 // the relocatable address and library names so host computers can use tools
151 // to symbolize and demangle (e.g., addr2line, c++filt).
152 char buf[1024];
153 strncpy(buf, "<unknown>", sizeof(buf));
154
155 // Subtract by one as return address of function may be in the next
156 // function when a function is annotated as noreturn.
157 void* address = static_cast<char*>(trace_[i]) - 1;
158 google::Symbolize(address, buf, sizeof(buf));
159
160 // TODO(scherkus): HACK - we blindly print contents of |buf| knowing that
scherkus (not reviewing) 2013/06/12 00:43:43 satorux: this is also really gross I want to use
satorux1 2013/06/12 05:43:42 If Symbolize() fails most of the time, using this
scherkus (not reviewing) 2013/06/13 01:27:44 I went with (2) -- let me know if you think it's t
161 // it's initialized to "<unknown>" and that our callback will be executed to
162 // fill the contents with the executable path and offset.
163
164 *os << " #" << std::setfill('0') << std::setw(2) << i << " " << buf
165 << "\n";
166 }
167
168 google::InstallSymbolizeCallback(NULL);
122 } 169 }
123 170
124 } // namespace debug 171 } // namespace debug
125 } // namespace base 172 } // namespace base
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | base/debug/stack_trace_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698