stack_trace_android.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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. 4 5#include "base/debug/stack_trace.h" 6 7#include <signal.h> 8#include <sys/types.h> 9#include <unistd.h> 10 11#include "base/logging.h" 12 13namespace base { 14namespace debug { 15 16bool 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); 26} 27 28StackTrace::StackTrace() { 29} 30 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. 34void 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); 38 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); 43 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); 50 51 // Restore the signal handler so that this method can work the next time. 52 signal(SIGSTKFLT, sig_handler); 53} 54 55void StackTrace::OutputToStream(std::ostream* os) const { 56 NOTIMPLEMENTED(); 57} 58 59} // namespace debug 60} // namespace base 61