Files

125 lines
3.3 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/SigSegv.h
*
* @brief Header file for libsigsegv wrapper.
*/
#if (!defined ACTIV_BASE_POSIX_SIG_SEGV_H)
#define ACTIV_BASE_POSIX_SIG_SEGV_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/CxxYZ/functional.h"
#include <sigsegv.h>
//! Activ namespace.
namespace Activ
{
/**
* @brief Helper to install a SIGSEGV handler.
*
* The callback will receive the fault address and should return true if the signal was handled.
*/
class SigSegvHandler : private Boost::noncopyable
{
public:
/**
* @brief Constructor.
*
* No handler is intalled.
*
* @param callback the function to invoke when a SIGSEGV occurs.
*/
template<typename T>
SigSegvHandler(const T &callback);
/**
* @brief Constructor.
*
* A handler is installed for the provided address and length.
*
* @param callback the function to invoke when a SIGSEGV occurs.
* @param pAddress the starting address to invoke @a callback when a SIGSEGV occurs.
* @param length the number of bytes from @a pAddress to protect.
*/
template<typename T>
SigSegvHandler(const T &callback, void * const pAddress, const size_t length);
//! Destructor. The handler is removed.
~SigSegvHandler();
/**
* @brief Install a handler for the provided address and length.
*
* Any existing handler is removed.
*
* @param pAddress the starting address to invoke the callback provided to the constructor when a SIGSEGV occurs.
* @param length the number of bytes from @a pAddress to protect.
*/
void Set(void * const pAddress, const size_t length);
//! Remove a handler.
void Reset();
private:
//! Function type for callback.
typedef CxxYZ::function<bool (void * const)> Callback;
ACTIV_BASE_API static int SegvHandler(void *pFault, void *pContext);
ACTIV_BASE_API static sigsegv_dispatcher *GetDispatcher();
const Callback m_callback;
void * m_pSigSegvHandler;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
inline SigSegvHandler::SigSegvHandler(const T &callback) :
m_callback(callback),
m_pSigSegvHandler(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
inline SigSegvHandler::SigSegvHandler(const T &callback, void * const pAddress, const size_t length) :
m_callback(callback),
m_pSigSegvHandler(0)
{
Set(pAddress, length);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline SigSegvHandler::~SigSegvHandler()
{
Reset();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void SigSegvHandler::Set(void * const pAddress, const size_t length)
{
Reset();
m_pSigSegvHandler = ::sigsegv_register(GetDispatcher(), pAddress, length, SegvHandler, this);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void SigSegvHandler::Reset()
{
if (0 != m_pSigSegvHandler)
{
::sigsegv_unregister(GetDispatcher(), m_pSigSegvHandler);
m_pSigSegvHandler = 0;
}
}
} // namespace Activ
#endif // (!defined ACTIV_BASE_POSIX_SIG_SEGV_H)