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
3 // found in the LICENSE file.
5 #include "base/debug/stack_trace.h"
11 #include "base/logging.h"
16 bool EnableInProcessStackDumping() {
17 // When running in an application, our code typically expects SIGPIPE
18 // to be ignored. Therefore, when testing that same code, it should run
19 // with SIGPIPE ignored as well.
20 // TODO(phajdan.jr): De-duplicate this SIGPIPE code.
21 struct sigaction action
;
22 memset(&action
, 0, sizeof(action
));
23 action
.sa_handler
= SIG_IGN
;
24 sigemptyset(&action
.sa_mask
);
25 return (sigaction(SIGPIPE
, &action
, NULL
) == 0);
28 StackTrace::StackTrace() {
31 // Sends fake SIGSTKFLT signals to let the Android linker and debuggerd dump
32 // stack. See inlined comments and Android bionic/linker/debugger.c and
33 // system/core/debuggerd/debuggerd.c for details.
34 void StackTrace::PrintBacktrace() const {
35 // Get the current handler of SIGSTKFLT for later use.
36 sighandler_t sig_handler
= signal(SIGSTKFLT
, SIG_DFL
);
37 signal(SIGSTKFLT
, sig_handler
);
39 // The Android linker will handle this signal and send a stack dumping request
40 // to debuggerd which will ptrace_attach this process. Before returning from
41 // the signal handler, the linker sets the signal handler to SIG_IGN.
42 kill(gettid(), SIGSTKFLT
);
44 // Because debuggerd will wait for the process to be stopped by the actual
45 // signal in crashing scenarios, signal is sent again to met the expectation.
46 // Debuggerd will dump stack into the system log and /data/tombstones/ files.
47 // NOTE: If this process runs in the interactive shell, it will be put
48 // in the background. To resume it in the foreground, use 'fg' command.
49 kill(gettid(), SIGSTKFLT
);
51 // Restore the signal handler so that this method can work the next time.
52 signal(SIGSTKFLT
, sig_handler
);
55 void StackTrace::OutputToStream(std::ostream
* os
) const {