|
Packit Service |
9def5d |
// Copyright (c) 2008, Google Inc.
|
|
Packit Service |
9def5d |
// All rights reserved.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
9def5d |
// modification, are permitted provided that the following conditions are
|
|
Packit Service |
9def5d |
// met:
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// * Redistributions of source code must retain the above copyright
|
|
Packit Service |
9def5d |
// notice, this list of conditions and the following disclaimer.
|
|
Packit Service |
9def5d |
// * Redistributions in binary form must reproduce the above
|
|
Packit Service |
9def5d |
// copyright notice, this list of conditions and the following disclaimer
|
|
Packit Service |
9def5d |
// in the documentation and/or other materials provided with the
|
|
Packit Service |
9def5d |
// distribution.
|
|
Packit Service |
9def5d |
// * Neither the name of Google Inc. nor the names of its
|
|
Packit Service |
9def5d |
// contributors may be used to endorse or promote products derived from
|
|
Packit Service |
9def5d |
// this software without specific prior written permission.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit Service |
9def5d |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit Service |
9def5d |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit Service |
9def5d |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit Service |
9def5d |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit Service |
9def5d |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit Service |
9def5d |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit Service |
9def5d |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit Service |
9def5d |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit Service |
9def5d |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit Service |
9def5d |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Author: Satoru Takabayashi
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// This is a helper binary for testing signalhandler.cc. The actual test
|
|
Packit Service |
9def5d |
// is done in signalhandler_unittest.sh.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include "utilities.h"
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include <pthread.h>
|
|
Packit Service |
9def5d |
#include <signal.h>
|
|
Packit Service |
9def5d |
#include <stdio.h>
|
|
Packit Service |
9def5d |
#include <stdlib.h>
|
|
Packit Service |
9def5d |
#include <string>
|
|
Packit Service |
9def5d |
#include "glog/logging.h"
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#ifdef HAVE_LIB_GFLAGS
|
|
Packit Service |
9def5d |
#include <gflags/gflags.h>
|
|
Packit Service |
9def5d |
using namespace GFLAGS_NAMESPACE;
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
using namespace GOOGLE_NAMESPACE;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
void* DieInThread(void*) {
|
|
Packit Service |
9def5d |
// We assume pthread_t is an integral number or a pointer, rather
|
|
Packit Service |
9def5d |
// than a complex struct. In some environments, pthread_self()
|
|
Packit Service |
9def5d |
// returns an uint64 but in some other environments pthread_self()
|
|
Packit Service |
9def5d |
// returns a pointer. Hence we use C-style cast here, rather than
|
|
Packit Service |
9def5d |
// reinterpret/static_cast, to support both types of environments.
|
|
Packit Service |
9def5d |
fprintf(stderr, "0x%lx is dying\n", (long)pthread_self());
|
|
Packit Service |
9def5d |
// Use volatile to prevent from these to be optimized away.
|
|
Packit Service |
9def5d |
volatile int a = 0;
|
|
Packit Service |
9def5d |
volatile int b = 1 / a;
|
|
Packit Service |
9def5d |
fprintf(stderr, "We should have died: b=%d\n", b);
|
|
Packit Service |
9def5d |
return NULL;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
void WriteToStdout(const char* data, int size) {
|
|
Packit Service |
9def5d |
if (write(STDOUT_FILENO, data, size) < 0) {
|
|
Packit Service |
9def5d |
// Ignore errors.
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
int main(int argc, char **argv) {
|
|
Packit Service |
9def5d |
#if defined(HAVE_STACKTRACE) && defined(HAVE_SYMBOLIZE)
|
|
Packit Service |
9def5d |
InitGoogleLogging(argv[0]);
|
|
Packit Service |
9def5d |
#ifdef HAVE_LIB_GFLAGS
|
|
Packit Service |
9def5d |
ParseCommandLineFlags(&argc, &argv, true);
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
InstallFailureSignalHandler();
|
|
Packit Service |
9def5d |
const std::string command = argc > 1 ? argv[1] : "none";
|
|
Packit Service |
9def5d |
if (command == "segv") {
|
|
Packit Service |
9def5d |
// We'll check if this is outputted.
|
|
Packit Service |
9def5d |
LOG(INFO) << "create the log file";
|
|
Packit Service |
9def5d |
LOG(INFO) << "a message before segv";
|
|
Packit Service |
9def5d |
// We assume 0xDEAD is not writable.
|
|
Packit Service |
9def5d |
int *a = (int*)0xDEAD;
|
|
Packit Service |
9def5d |
*a = 0;
|
|
Packit Service |
9def5d |
} else if (command == "loop") {
|
|
Packit Service |
9def5d |
fprintf(stderr, "looping\n");
|
|
Packit Service |
9def5d |
while (true);
|
|
Packit Service |
9def5d |
} else if (command == "die_in_thread") {
|
|
Packit Service |
9def5d |
pthread_t thread;
|
|
Packit Service |
9def5d |
pthread_create(&thread, NULL, &DieInThread, NULL);
|
|
Packit Service |
9def5d |
pthread_join(thread, NULL);
|
|
Packit Service |
9def5d |
} else if (command == "dump_to_stdout") {
|
|
Packit Service |
9def5d |
InstallFailureWriter(WriteToStdout);
|
|
Packit Service |
9def5d |
abort();
|
|
Packit Service |
9def5d |
} else {
|
|
Packit Service |
9def5d |
// Tell the shell script
|
|
Packit Service |
9def5d |
puts("OK");
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
return 0;
|
|
Packit Service |
9def5d |
}
|