// Copyright 2012 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/// Ensures that the given expression does not return a kyua_error_t.
///
/// \param expr Expression to evaluate.
#define RE(expr) ATF_REQUIRE(!kyua_error_is_set(expr))
/// Ensures that the given expression does not return a kyua_error_t.
///
/// \param expr Expression to evaluate.
/// \param msg Failure message.
#define RE_MSG(expr, msg) ATF_REQUIRE_MSG(!kyua_error_is_set(expr), msg)
/// Generates a core dump.
///
/// Due to the complexity of this interface, you should probably use
/// generate_core() instead.
///
/// \post If this fails to generate a core file, the test case is marked as
/// skipped. The caller therefore can rely that a core dump has been created on
/// return.
///
/// \param tc Pointer to the caller test case.
/// \param run_params Parameters for the execution of the helper.
/// \param helper_path Path to the created helper.
/// \param exec_path Name of the helper, prefixed with ./ so that it can be
/// executed from within the work directory.
/// \param helper_name Basename of the helper.
///
/// \return The PID of the crashed binary.
static pid_t
generate_core_aux(const atf_tc_t* tc, const kyua_run_params_t* run_params,
const char* helper_path, const char* exec_path,
const char* helper_name)
{
const char* srcdir = atf_tc_get_config_var(tc, "srcdir");
// We use kyua_run_fork for this to better simulate the final use case of
// the stacktrace gathering, as test programs are run through these
// functions. Also, kyua_run_fork provides us with automatic unlimiting of
// resources so that core files can be generated.
int status; bool timed_out;
RE_MSG(kyua_run_wait(pid, &status, &timed_out),
"wait failed; unexpected problem during exec?");
ATF_REQUIRE(WIFSIGNALED(status));
if (!WCOREDUMP(status))
atf_tc_skip("Test failed to generate core dump");
return pid;
}
/// Creates a script.
///
/// \param script Path to the script to create.
/// \param contents Contents of the script.
static void
create_script(const char* script, const char* contents)
{
atf_utils_create_file(script, "#! /bin/sh\n\n%s\n", contents);
ATF_REQUIRE(chmod(script, 0755) != -1);
}
/// Generates a core file.
///
/// \param tc Pointer to the calling test case.
/// \param work_directory Name of the directory in which to place the binary
/// that will generate the stacktrace.
/// \param program_name Basename of the binary that will crash.
///
/// \return PID of the process that generated the core file.
static pid_t
generate_core(const atf_tc_t* tc, const char* work_directory,
const char* program_name)
{
kyua_run_params_t run_params;
kyua_run_params_init(&run_params);
if (strcmp(work_directory, ".") != 0) {
ATF_REQUIRE(mkdir(work_directory, 0755) != -1);
run_params.work_directory = work_directory;
}
/// Prepares and runs kyua_stacktrace_dump().
///
/// \param tc Pointer to the calling test case.
/// \param work_directory Name of the directory in which to place the binary
/// that will generate the stacktrace.
/// \param program_name Basename of the binary that will crash.
/// \param output_name Name of the file to which to write the stacktrace.
/// \param timeout_seconds Time to give GDB to complete.
static void
do_dump(const atf_tc_t* tc, const char* work_directory,
const char* program_name, const char* output_name,
const int timeout_seconds)
{
const pid_t pid = generate_core(tc, work_directory, program_name);
kyua_run_params_t run_params;
kyua_run_params_init(&run_params);
run_params.timeout_seconds = timeout_seconds + 100; // Some large value.
run_params.work_directory = work_directory; // Created by generate_core.
// It is hard to validate the execution of an arbitrary GDB of which we know
// nothing anything. Just assume that the backtrace, at the very least,
// prints a frame identifier.
ATF_REQUIRE(atf_utils_grep_file("#0", "stacktrace"));
}