Commit 198a6d51 authored by 's avatar

Forgot to add file for the previous change...

Add InstallFailureSignalHandler().  The function installs a signal handler that will dump useful information when the program crashes on certain signals such as SIGSEGV.


git-svn-id: https://google-glog.googlecode.com/svn/trunk@12 eb4d4688-79bd-11dd-afb4-1d65580434c0
parent 91d9fd88
AC_DEFUN([AX_C___SYNC_VAL_COMPARE_AND_SWAP], [
AC_MSG_CHECKING(for __sync_val_compare_and_swap)
AC_CACHE_VAL(ac_cv___sync_val_compare_and_swap, [
AC_TRY_LINK(
[int main() { int a; if (__sync_val_compare_and_swap(&a, 0, 1)) return 1; return 0; }],
[],
ac_cv___sync_val_compare_and_swap=yes,
ac_cv___sync_val_compare_and_swap=no
)])
if test "$ac_cv___sync_val_compare_and_swap" = "yes"; then
AC_DEFINE(HAVE___SYNC_VAL_COMPARE_AND_SWAP, 1, [define if your compiler has __sync_val_compare_and_swap])
fi
AC_MSG_RESULT($ac_cv___sync_val_compare_and_swap)
])
This diff is collapsed.
// Copyright 2008 Google Inc. All Rights Reserved.
// Author: satorux@google.com (Satoru Takabayashi)
//
// This is a helper binary for testing signalhandler.cc. The actual test
// is done in signalhandler_unittest.sh.
#include "utilities.h"
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "glog/logging.h"
using namespace GOOGLE_NAMESPACE;
void* DieInThread(void*) {
fprintf(stderr, "0x%lx is dying\n", pthread_self());
// Use volatile to prevent from these to be optimized away.
volatile int a = 0;
volatile int b = 1 / a;
}
void WriteToStdout(const char* data, int size) {
write(STDOUT_FILENO, data, size);
}
int main(int argc, char **argv) {
#ifdef HAVE_STACKTRACE
InitGoogleLogging(argv[0]);
InstallFailureSignalHandler();
const std::string command = argc > 1 ? argv[1] : "none";
if (command == "segv") {
// We assume 0xDEAD is not writable.
int *a = (int*)0xDEAD;
*a = 0;
} else if (command == "loop") {
fprintf(stderr, "looping\n");
while (true);
} else if (command == "die_in_thread") {
pthread_t thread;
pthread_create(&thread, NULL, &DieInThread, NULL);
pthread_join(thread, NULL);
} else if (command == "dump_to_stdout") {
InstallFailureWriter(WriteToStdout);
abort();
} else {
// Tell the shell script
puts("OK");
}
#endif
return 0;
}
#! /bin/sh
# Copyright 2008 Google, Inc. All Rights Reserved.
# Author: Satoru Takabayashi
#
# Unit tests for signalhandler.cc.
die () {
echo $1
exit 1
}
BINDIR=".libs"
LIBGLOG="$BINDIR/libglog.so"
BINARY="$BINDIR/signalhandler_unittest"
# Remove temporary files.
rm -f signalhandler.out*
if test -e "$BINARY"; then
# We need shared object.
export LD_LIBRARY_PATH=$BINDIR
export DYLD_LIBRARY_PATH=$BINDIR
else
# For windows
BINARY="./signalhandler_unittest.exe"
if ! test -e "$BINARY"; then
echo "We coundn't find demangle_unittest binary."
exit 1
fi
fi
if [ x`$BINARY` != 'xOK' ]; then
echo "PASS (No stacktrace support. We don't run this test.)"
exit 0
fi
# Test for a case the program kills itself by SIGSEGV.
$BINARY segv 2> signalhandler.out1
for pattern in SIGSEGV 0xdead main "Aborted at [0-9]"; do
if ! grep --quiet "$pattern" signalhandler.out1; then
die "'$pattern' should appear in the output"
fi
done
# Test for a case the program is killed by this shell script.
# $! = the process id of the last command run in the background.
# $$ = the process id of this shell.
$BINARY loop 2> signalhandler.out2 &
# Wait until "looping" is written in the file. This indicates the program
# is ready to accept signals.
while true; do
if grep --quiet looping signalhandler.out2; then
break
fi
done
kill -TERM $!
wait $!
from_pid=''
# Only linux has the process ID of the signal sender.
if [ x`uname` = "xLinux" ]; then
from_pid="from PID $$"
fi
for pattern in SIGTERM "by PID $!" "$from_pid" main "Aborted at [0-9]"; do
if ! grep --quiet "$pattern" signalhandler.out2; then
die "'$pattern' should appear in the output"
fi
done
# Test for a case the program dies in a non-main thread.
$BINARY die_in_thread 2> signalhandler.out3
EXPECTED_TID="`sed 's/ .*//' signalhandler.out3`"
for pattern in SIGFPE DieInThread "TID $EXPECTED_TID" "Aborted at [0-9]"; do
if ! grep --quiet "$pattern" signalhandler.out3; then
die "'$pattern' should appear in the output"
fi
done
# Test for a case the program installs a custom failure writer that writes
# stuff to stdout instead of stderr.
$BINARY dump_to_stdout 1> signalhandler.out4
for pattern in SIGABRT main "Aborted at [0-9]"; do
if ! grep --quiet "$pattern" signalhandler.out4; then
die "'$pattern' should appear in the output"
fi
done
echo PASS
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment