📄 execution_monitor.cpp
字号:
// (C) Copyright Gennadiy Rozental 2001-2003.
// (C) Copyright Beman Dawes and Ullrich Koethe 1995-2001.
// Use, modification, and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org/libs/test for the library home page.
//
// File : $RCSfile: execution_monitor.cpp,v $
//
// Version : $Revision: 1.30 $
//
// Description : provides execution monitor implementation for all supported
// configurations, including Microsoft structured exception based, unix signals
// based and special workarounds for borland
//
// Note that when testing requirements or user wishes preclude use of this
// file as a separate compilation unit, it may be included as a header file.
//
// Header dependencies are deliberately restricted to reduce coupling to other
// boost libraries.
// ***************************************************************************
// Boost.Test
#include <boost/test/execution_monitor.hpp>
// BOOST
#include <boost/cstdlib.hpp> // for exit codes
#include <boost/config.hpp> // for workarounds
// STL
#include <string> // for string
#include <new> // for bad_alloc
#include <typeinfo> // for bad_cast, bad_typeid
#include <exception> // for exception, bad_exception
#include <stdexcept> // for std exception hierarchy
#include <cstring> // for C string API
#include <cassert> // for assert
#include <cstddef> // for NULL
#ifdef BOOST_NO_STDC_NAMESPACE
namespace std { using ::strlen; using ::strncat; }
#endif
// Microsoft + other compatible compilers such as Intel
#if !defined(BOOST_DISABLE_WIN32) && \
!defined(__BORLANDC__) && \
(defined(_MSC_VER) && !defined(__COMO__)) || \
(defined(__INTEL__) && defined(__MWERKS__) && __MWERKS__ >= 0x3000)
#define BOOST_MS_STRCTURED_EXCEPTION_HANDLING
#include <wtypes.h>
#include <winbase.h>
#include <excpt.h>
#include <eh.h>
#if !defined(NDEBUG) && !defined(__MWERKS__) // __MWERKS__ does not seem to supply implementation of C runtime debug hooks, causing linking errors
#define BOOST_MS_CRT_DEBUG_HOOK
#include <crtdbg.h>
#endif
#elif (defined(__BORLANDC__) && defined(_Windows) && !defined(BOOST_DISABLE_WIN32))
#define BOOST_MS_STRCTURED_EXCEPTION_HANDLING
#include <windows.h> // Borland 5.5.1 has its own way of doing things.
#elif defined(BOOST_HAS_SIGACTION)
#define BOOST_SIGACTION_BASED_SIGNAL_HANDLING
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
#else
#define BOOST_NO_SIGNAL_HANDLING
#endif
namespace boost {
namespace detail {
using unit_test_framework::c_string_literal;
// boost::execution_monitor::execute() calls boost::detail::catch_signals() to
// execute user function with signals control
// boost::execution_monitor::execute() calls boost::detail::report_error(...) to
// report any caught exception and throw execution_exception
const std::size_t REPORT_ERROR_BUFFER_SIZE = 512;
static int catch_signals( execution_monitor & exmon, bool catch_system_errors, int timeout ); // timeout is in seconds. 0 implies none.
static void report_error( execution_exception::error_code ec,
c_string_literal msg1, // first part of the message
c_string_literal msg2 = "" ); // second part of the message; sum length msg1 + msg2 should
// exceed REPORT_ERROR_BUFFER_SIZE; never concatenate messages
// manually, cause it should work even in case of memory lack
//____________________________________________________________________________//
// Declaration for Microsoft structured exception handling (unix alternative - signal)
#ifdef BOOST_MS_STRCTURED_EXCEPTION_HANDLING
// this class defined per the Microsoft structured exception documentation
class ms_se_exception {
public:
// Constructor
explicit ms_se_exception( unsigned int n )
: m_se_id( n ) {}
// Destructor
~ms_se_exception() {}
// access methods
unsigned int id() const { return m_se_id; }
private:
// Data members
unsigned int m_se_id;
};
//____________________________________________________________________________//
void ms_se_trans_func( unsigned int id, _EXCEPTION_POINTERS* exps );
void ms_se_forward_func( unsigned int id, _EXCEPTION_POINTERS* exps );
static void report_ms_se_error( unsigned int id );
//____________________________________________________________________________//
// Declarations for unix-style signal handling
#elif defined(BOOST_SIGACTION_BASED_SIGNAL_HANDLING)
class unix_signal_exception {
public:
// Constructor
unix_signal_exception( execution_exception::error_code ec, c_string_literal em )
: m_error_code( ec ), m_error_message( em ) {}
// Destructor
~unix_signal_exception() {}
// access methods
execution_exception::error_code error_code() const { return m_error_code; }
c_string_literal error_message() const { return m_error_message; }
private:
// Data members
execution_exception::error_code m_error_code;
c_string_literal m_error_message;
};
#endif
//____________________________________________________________________________//
#if defined(BOOST_MS_CRT_DEBUG_HOOK)
int
assert_reporting_function( int reportType, char* userMessage, int* retVal )
{
switch( reportType ) {
case _CRT_ASSERT:
detail::report_error( execution_exception::user_error, userMessage );
return 1; // return value and retVal are not important since we never reach this line
case _CRT_ERROR:
detail::report_error( execution_exception::system_error, userMessage );
return 1; // return value and retVal are not important since we never reach this line
default:
return 0; // use usual reporting method
}
}
#endif
} // namespace detail
// ************************************************************************** //
// ************** execution_monitor ************** //
// ************************************************************************** //
int
execution_monitor::run_function()
{
return m_custom_translators ? (*m_custom_translators)( *this ) : function();
}
//____________________________________________________________________________//
int
execution_monitor::execute( bool catch_system_errors, int timeout )
{
using unit_test_framework::c_string_literal;
#if defined(BOOST_MS_STRCTURED_EXCEPTION_HANDLING) && !defined(__BORLANDC__)
if( catch_system_errors )
_set_se_translator( detail::ms_se_trans_func );
else
_set_se_translator( detail::ms_se_forward_func );
#endif
#if defined(BOOST_MS_CRT_DEBUG_HOOK)
if( catch_system_errors )
_CrtSetReportHook( &detail::assert_reporting_function );
#endif
try {
return detail::catch_signals( *this, catch_system_errors, timeout );
}
// Catch-clause reference arguments are a bit different from function
// arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't
// required. Programmers ask for const anyhow, so we supply it. That's
// easier than answering questions about non-const usage.
catch( c_string_literal ex )
{ detail::report_error( execution_exception::cpp_exception_error, "C string: ", ex ); }
catch( std::string const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::string: ", ex.c_str() ); }
// std:: exceptions
catch( std::bad_alloc const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::bad_alloc: ", ex.what() ); }
#if !defined(__BORLANDC__) || __BORLANDC__ > 0x0551
catch( std::bad_cast const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::bad_cast: ", ex.what() ); }
catch( std::bad_typeid const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::bad_typeid: ", ex.what() ); }
#else
catch( std::bad_cast const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::bad_cast" ); }
catch( std::bad_typeid const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::bad_typeid" ); }
#endif
catch( std::bad_exception const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::bad_exception: ", ex.what() ); }
catch( std::domain_error const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::domain_error: ", ex.what() ); }
catch( std::invalid_argument const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::invalid_argument: ", ex.what() ); }
catch( std::length_error const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::length_error: ", ex.what() ); }
catch( std::out_of_range const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::out_of_range: ", ex.what() ); }
catch( std::range_error const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::range_error: ", ex.what() ); }
catch( std::overflow_error const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::overflow_error: ", ex.what() ); }
catch( std::underflow_error const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::underflow_error: ", ex.what() ); }
catch( std::logic_error const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::logic_error: ", ex.what() ); }
catch( std::runtime_error const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::runtime_error: ", ex.what() ); }
catch( std::exception const& ex )
{ detail::report_error( execution_exception::cpp_exception_error, "std::exception: ", ex.what() ); }
#if defined(BOOST_MS_STRCTURED_EXCEPTION_HANDLING)
catch( detail::ms_se_exception const& ex )
{ detail::report_ms_se_error( ex.id() ); }
#elif defined(BOOST_SIGACTION_BASED_SIGNAL_HANDLING)
catch( detail::unix_signal_exception const& ex )
{ detail::report_error( ex.error_code(), ex.error_message() ); }
#endif // BOOST_SIGACTION_BASED_SIGNAL_HANDLING
catch( execution_exception const& ) { throw; }
catch( ... )
{ detail::report_error( execution_exception::cpp_exception_error, "unknown type" ); }
return 0; // never reached; supplied to quiet compiler warnings
} // execute
//____________________________________________________________________________//
namespace detail {
#if defined(BOOST_SIGACTION_BASED_SIGNAL_HANDLING)
// ************************************************************************** //
// ************** boost::detail::signal_handler ************** //
// ************************************************************************** //
class signal_handler {
public:
// Constructor
explicit signal_handler( bool catch_system_errors, int timeout );
// Destructor
~signal_handler();
// access methods
static sigjmp_buf& jump_buffer()
{
assert( s_active_handler );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -