execution_monitor.ipp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // (C) Copyright Beman Dawes and Ullrich Koethe 1995-2001.
  3. // Use, modification, and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/test for the library home page.
  7. //
  8. /// @file
  9. /// Provides execution monitor implementation for all supported
  10. /// configurations, including Microsoft structured exception based, unix signals
  11. /// based and special workarounds for borland
  12. ///
  13. /// Note that when testing requirements or user wishes preclude use of this
  14. /// file as a separate compilation unit, it may be included as a header file.
  15. ///
  16. /// Header dependencies are deliberately restricted to reduce coupling to other
  17. /// boost libraries.
  18. // ***************************************************************************
  19. #ifndef BOOST_TEST_EXECUTION_MONITOR_IPP_012205GER
  20. #define BOOST_TEST_EXECUTION_MONITOR_IPP_012205GER
  21. // Boost.Test
  22. #include <boost/test/detail/config.hpp>
  23. #include <boost/test/detail/throw_exception.hpp>
  24. #include <boost/test/execution_monitor.hpp>
  25. #include <boost/test/debug.hpp>
  26. // Boost
  27. #include <boost/cstdlib.hpp> // for exit codes
  28. #include <boost/config.hpp> // for workarounds
  29. #include <boost/core/ignore_unused.hpp> // for ignore_unused
  30. #ifndef BOOST_NO_EXCEPTIONS
  31. #include <boost/exception/get_error_info.hpp> // for get_error_info
  32. #include <boost/exception/current_exception_cast.hpp> // for current_exception_cast
  33. #include <boost/exception/diagnostic_information.hpp>
  34. #endif
  35. // STL
  36. #include <string> // for std::string
  37. #include <new> // for std::bad_alloc
  38. #include <typeinfo> // for std::bad_cast, std::bad_typeid
  39. #include <exception> // for std::exception, std::bad_exception
  40. #include <stdexcept> // for std exception hierarchy
  41. #include <cstring> // for C string API
  42. #include <cassert> // for assert
  43. #include <cstddef> // for NULL
  44. #include <cstdio> // for vsnprintf
  45. #include <stdio.h>
  46. #include <cstdarg> // for varargs
  47. #include <stdarg.h>
  48. #include <cmath> // for ceil
  49. #include <iostream> // for varargs
  50. #ifdef BOOST_NO_STDC_NAMESPACE
  51. namespace std { using ::strerror; using ::strlen; using ::strncat; using ::ceil; }
  52. #endif
  53. // to use vsnprintf
  54. #if defined(__SUNPRO_CC) || defined(__SunOS) || defined(__QNXNTO__) || defined(__VXWORKS__)
  55. using std::va_list;
  56. #endif
  57. #if defined(__VXWORKS__)
  58. # define BOOST_TEST_LIMITED_SIGNAL_DETAILS
  59. #endif
  60. #ifdef BOOST_SEH_BASED_SIGNAL_HANDLING
  61. # if !defined(_WIN32_WINNT) // WinXP
  62. # define _WIN32_WINNT 0x0501
  63. # endif
  64. # include <windows.h>
  65. # if defined(__MWERKS__) || (defined(_MSC_VER) && !defined(UNDER_CE))
  66. # include <eh.h>
  67. # endif
  68. # if defined(__BORLANDC__) && __BORLANDC__ >= 0x560 || defined(__MWERKS__)
  69. # include <stdint.h>
  70. # endif
  71. # if defined(__BORLANDC__) && __BORLANDC__ < 0x560
  72. typedef unsigned uintptr_t;
  73. # endif
  74. # if defined(UNDER_CE) && BOOST_WORKAROUND(_MSC_VER, < 1500 )
  75. typedef void* uintptr_t;
  76. # elif defined(UNDER_CE)
  77. # include <crtdefs.h>
  78. # endif
  79. # if !defined(NDEBUG) && defined(_MSC_VER) && !defined(UNDER_CE)
  80. # include <crtdbg.h>
  81. # define BOOST_TEST_CRT_HOOK_TYPE _CRT_REPORT_HOOK
  82. # define BOOST_TEST_CRT_ASSERT _CRT_ASSERT
  83. # define BOOST_TEST_CRT_ERROR _CRT_ERROR
  84. # define BOOST_TEST_CRT_SET_HOOK(H) _CrtSetReportHook(H)
  85. # else
  86. # define BOOST_TEST_CRT_HOOK_TYPE void*
  87. # define BOOST_TEST_CRT_ASSERT 2
  88. # define BOOST_TEST_CRT_ERROR 1
  89. # define BOOST_TEST_CRT_SET_HOOK(H) (void*)(H)
  90. # endif
  91. # if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0501) /* WinXP */
  92. # define BOOST_TEST_WIN32_WAITABLE_TIMERS
  93. # endif
  94. # if (!BOOST_WORKAROUND(_MSC_VER, >= 1400 ) && \
  95. !defined(BOOST_COMO)) || defined(UNDER_CE)
  96. typedef void* _invalid_parameter_handler;
  97. inline _invalid_parameter_handler
  98. _set_invalid_parameter_handler( _invalid_parameter_handler arg )
  99. {
  100. return arg;
  101. }
  102. # endif
  103. # if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0564)) || defined(UNDER_CE)
  104. namespace { void _set_se_translator( void* ) {} }
  105. # endif
  106. #elif defined(BOOST_HAS_SIGACTION)
  107. # define BOOST_SIGACTION_BASED_SIGNAL_HANDLING
  108. # include <unistd.h>
  109. # include <signal.h>
  110. # include <setjmp.h>
  111. # if defined(__FreeBSD__)
  112. # include <osreldate.h>
  113. # ifndef SIGPOLL
  114. # define SIGPOLL SIGIO
  115. # endif
  116. # if (__FreeBSD_version < 70100)
  117. # define ILL_ILLADR 0 // ILL_RESAD_FAULT
  118. # define ILL_PRVOPC ILL_PRIVIN_FAULT
  119. # define ILL_ILLOPN 2 // ILL_RESOP_FAULT
  120. # define ILL_COPROC ILL_FPOP_FAULT
  121. # define BOOST_TEST_LIMITED_SIGNAL_DETAILS
  122. # endif
  123. # endif
  124. # if defined(__ANDROID__)
  125. # include <android/api-level.h>
  126. # endif
  127. // documentation of BOOST_TEST_DISABLE_ALT_STACK in execution_monitor.hpp
  128. # if !defined(__CYGWIN__) && !defined(__QNXNTO__) && !defined(__bgq__) && \
  129. (!defined(__ANDROID__) || __ANDROID_API__ >= 8) && \
  130. !defined(BOOST_TEST_DISABLE_ALT_STACK)
  131. # define BOOST_TEST_USE_ALT_STACK
  132. # endif
  133. # if defined(SIGPOLL) && !defined(__CYGWIN__) && \
  134. !(defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) && \
  135. !defined(__NetBSD__) && \
  136. !defined(__QNXNTO__)
  137. # define BOOST_TEST_CATCH_SIGPOLL
  138. # endif
  139. # ifdef BOOST_TEST_USE_ALT_STACK
  140. # define BOOST_TEST_ALT_STACK_SIZE SIGSTKSZ
  141. # endif
  142. #else
  143. # define BOOST_NO_SIGNAL_HANDLING
  144. #endif
  145. #ifndef UNDER_CE
  146. #include <errno.h>
  147. #endif
  148. #if !defined(BOOST_NO_TYPEID) && !defined(BOOST_NO_RTTI)
  149. # include <boost/core/demangle.hpp>
  150. #endif
  151. #include <boost/test/detail/suppress_warnings.hpp>
  152. //____________________________________________________________________________//
  153. namespace boost {
  154. // ************************************************************************** //
  155. // ************** throw_exception ************** //
  156. // ************************************************************************** //
  157. #ifdef BOOST_NO_EXCEPTIONS
  158. void throw_exception( std::exception const & e ) { abort(); }
  159. #endif
  160. // ************************************************************************** //
  161. // ************** report_error ************** //
  162. // ************************************************************************** //
  163. namespace detail {
  164. #ifdef __BORLANDC__
  165. # define BOOST_TEST_VSNPRINTF( a1, a2, a3, a4 ) std::vsnprintf( (a1), (a2), (a3), (a4) )
  166. #elif BOOST_WORKAROUND(_MSC_VER, <= 1310) || \
  167. BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3000)) || \
  168. defined(UNDER_CE) || \
  169. (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  170. # define BOOST_TEST_VSNPRINTF( a1, a2, a3, a4 ) _vsnprintf( (a1), (a2), (a3), (a4) )
  171. #else
  172. # define BOOST_TEST_VSNPRINTF( a1, a2, a3, a4 ) vsnprintf( (a1), (a2), (a3), (a4) )
  173. #endif
  174. #ifndef BOOST_NO_EXCEPTIONS
  175. template <typename ErrorInfo>
  176. typename ErrorInfo::value_type
  177. extract( boost::exception const* ex )
  178. {
  179. if( !ex )
  180. return 0;
  181. typename ErrorInfo::value_type const * val = boost::get_error_info<ErrorInfo>( *ex );
  182. return val ? *val : 0;
  183. }
  184. //____________________________________________________________________________//
  185. static void
  186. report_error( execution_exception::error_code ec, boost::exception const* be, char const* format, va_list* args )
  187. {
  188. static const int REPORT_ERROR_BUFFER_SIZE = 4096;
  189. static char buf[REPORT_ERROR_BUFFER_SIZE];
  190. BOOST_TEST_VSNPRINTF( buf, sizeof(buf)-1, format, *args );
  191. buf[sizeof(buf)-1] = 0;
  192. va_end( *args );
  193. BOOST_TEST_I_THROW(execution_exception( ec, buf, execution_exception::location( extract<throw_file>( be ),
  194. (size_t)extract<throw_line>( be ),
  195. extract<throw_function>( be ) ) ));
  196. }
  197. //____________________________________________________________________________//
  198. static void
  199. report_error( execution_exception::error_code ec, boost::exception const* be, char const* format, ... )
  200. {
  201. va_list args;
  202. va_start( args, format );
  203. report_error( ec, be, format, &args );
  204. }
  205. #endif
  206. //____________________________________________________________________________//
  207. static void
  208. report_error( execution_exception::error_code ec, char const* format, ... )
  209. {
  210. va_list args;
  211. va_start( args, format );
  212. report_error( ec, 0, format, &args );
  213. }
  214. //____________________________________________________________________________//
  215. template<typename Tr,typename Functor>
  216. inline int
  217. do_invoke( Tr const& tr, Functor const& F )
  218. {
  219. return tr ? (*tr)( F ) : F();
  220. }
  221. //____________________________________________________________________________//
  222. struct fpe_except_guard {
  223. explicit fpe_except_guard( unsigned detect_fpe )
  224. : m_detect_fpe( detect_fpe )
  225. {
  226. // prepare fp exceptions control
  227. m_previously_enabled = fpe::disable( fpe::BOOST_FPE_ALL );
  228. if( m_previously_enabled != fpe::BOOST_FPE_INV && detect_fpe != fpe::BOOST_FPE_OFF )
  229. fpe::enable( detect_fpe );
  230. }
  231. ~fpe_except_guard()
  232. {
  233. if( m_detect_fpe != fpe::BOOST_FPE_OFF )
  234. fpe::disable( m_detect_fpe );
  235. if( m_previously_enabled != fpe::BOOST_FPE_INV )
  236. fpe::enable( m_previously_enabled );
  237. }
  238. unsigned m_detect_fpe;
  239. unsigned m_previously_enabled;
  240. };
  241. // ************************************************************************** //
  242. // ************** typeid_name ************** //
  243. // ************************************************************************** //
  244. #if !defined(BOOST_NO_TYPEID) && !defined(BOOST_NO_RTTI)
  245. template<typename T>
  246. std::string
  247. typeid_name( T const& t )
  248. {
  249. return boost::core::demangle(typeid(t).name());
  250. }
  251. #endif
  252. } // namespace detail
  253. #if defined(BOOST_SIGACTION_BASED_SIGNAL_HANDLING)
  254. // ************************************************************************** //
  255. // ************** Sigaction based signal handling ************** //
  256. // ************************************************************************** //
  257. namespace detail {
  258. // ************************************************************************** //
  259. // ************** boost::detail::system_signal_exception ************** //
  260. // ************************************************************************** //
  261. class system_signal_exception {
  262. public:
  263. // Constructor
  264. system_signal_exception()
  265. : m_sig_info( 0 )
  266. , m_context( 0 )
  267. {}
  268. // Access methods
  269. void operator()( siginfo_t* i, void* c )
  270. {
  271. m_sig_info = i;
  272. m_context = c;
  273. }
  274. void report() const;
  275. private:
  276. // Data members
  277. siginfo_t* m_sig_info; // system signal detailed info
  278. void* m_context; // signal context
  279. };
  280. //____________________________________________________________________________//
  281. void
  282. system_signal_exception::report() const
  283. {
  284. if( !m_sig_info )
  285. return; // no error actually occur?
  286. switch( m_sig_info->si_code ) {
  287. #ifdef __VXWORKS__
  288. // a bit of a hack to adapt code to small m_sig_info VxWorks uses
  289. #define si_addr si_value.sival_int
  290. #define si_band si_value.sival_int
  291. #else
  292. case SI_USER:
  293. report_error( execution_exception::system_error,
  294. "signal: generated by kill() (or family); uid=%d; pid=%d",
  295. (int)m_sig_info->si_uid, (int)m_sig_info->si_pid );
  296. break;
  297. #endif
  298. case SI_QUEUE:
  299. report_error( execution_exception::system_error,
  300. "signal: sent by sigqueue()" );
  301. break;
  302. case SI_TIMER:
  303. report_error( execution_exception::system_error,
  304. "signal: the expiration of a timer set by timer_settimer()" );
  305. break;
  306. case SI_ASYNCIO:
  307. report_error( execution_exception::system_error,
  308. "signal: generated by the completion of an asynchronous I/O request" );
  309. break;
  310. case SI_MESGQ:
  311. report_error( execution_exception::system_error,
  312. "signal: generated by the the arrival of a message on an empty message queue" );
  313. break;
  314. default:
  315. break;
  316. }
  317. switch( m_sig_info->si_signo ) {
  318. case SIGILL:
  319. switch( m_sig_info->si_code ) {
  320. #ifndef BOOST_TEST_LIMITED_SIGNAL_DETAILS
  321. case ILL_ILLOPC:
  322. report_error( execution_exception::system_fatal_error,
  323. "signal: illegal opcode; address of failing instruction: 0x%08lx",
  324. m_sig_info->si_addr );
  325. break;
  326. case ILL_ILLTRP:
  327. report_error( execution_exception::system_fatal_error,
  328. "signal: illegal trap; address of failing instruction: 0x%08lx",
  329. m_sig_info->si_addr );
  330. break;
  331. case ILL_PRVREG:
  332. report_error( execution_exception::system_fatal_error,
  333. "signal: privileged register; address of failing instruction: 0x%08lx",
  334. m_sig_info->si_addr );
  335. break;
  336. case ILL_BADSTK:
  337. report_error( execution_exception::system_fatal_error,
  338. "signal: internal stack error; address of failing instruction: 0x%08lx",
  339. m_sig_info->si_addr );
  340. break;
  341. #endif
  342. case ILL_ILLOPN:
  343. report_error( execution_exception::system_fatal_error,
  344. "signal: illegal operand; address of failing instruction: 0x%08lx",
  345. m_sig_info->si_addr );
  346. break;
  347. case ILL_ILLADR:
  348. report_error( execution_exception::system_fatal_error,
  349. "signal: illegal addressing mode; address of failing instruction: 0x%08lx",
  350. m_sig_info->si_addr );
  351. break;
  352. case ILL_PRVOPC:
  353. report_error( execution_exception::system_fatal_error,
  354. "signal: privileged opcode; address of failing instruction: 0x%08lx",
  355. m_sig_info->si_addr );
  356. break;
  357. case ILL_COPROC:
  358. report_error( execution_exception::system_fatal_error,
  359. "signal: co-processor error; address of failing instruction: 0x%08lx",
  360. m_sig_info->si_addr );
  361. break;
  362. default:
  363. report_error( execution_exception::system_fatal_error,
  364. "signal: SIGILL, si_code: %d (illegal instruction; address of failing instruction: 0x%08lx)",
  365. m_sig_info->si_addr, m_sig_info->si_code );
  366. break;
  367. }
  368. break;
  369. case SIGFPE:
  370. switch( m_sig_info->si_code ) {
  371. case FPE_INTDIV:
  372. report_error( execution_exception::system_error,
  373. "signal: integer divide by zero; address of failing instruction: 0x%08lx",
  374. m_sig_info->si_addr );
  375. break;
  376. case FPE_INTOVF:
  377. report_error( execution_exception::system_error,
  378. "signal: integer overflow; address of failing instruction: 0x%08lx",
  379. m_sig_info->si_addr );
  380. break;
  381. case FPE_FLTDIV:
  382. report_error( execution_exception::system_error,
  383. "signal: floating point divide by zero; address of failing instruction: 0x%08lx",
  384. m_sig_info->si_addr );
  385. break;
  386. case FPE_FLTOVF:
  387. report_error( execution_exception::system_error,
  388. "signal: floating point overflow; address of failing instruction: 0x%08lx",
  389. m_sig_info->si_addr );
  390. break;
  391. case FPE_FLTUND:
  392. report_error( execution_exception::system_error,
  393. "signal: floating point underflow; address of failing instruction: 0x%08lx",
  394. m_sig_info->si_addr );
  395. break;
  396. case FPE_FLTRES:
  397. report_error( execution_exception::system_error,
  398. "signal: floating point inexact result; address of failing instruction: 0x%08lx",
  399. m_sig_info->si_addr );
  400. break;
  401. case FPE_FLTINV:
  402. report_error( execution_exception::system_error,
  403. "signal: invalid floating point operation; address of failing instruction: 0x%08lx",
  404. m_sig_info->si_addr );
  405. break;
  406. case FPE_FLTSUB:
  407. report_error( execution_exception::system_error,
  408. "signal: subscript out of range; address of failing instruction: 0x%08lx",
  409. m_sig_info->si_addr );
  410. break;
  411. default:
  412. report_error( execution_exception::system_error,
  413. "signal: SIGFPE, si_code: %d (errnoneous arithmetic operations; address of failing instruction: 0x%08lx)",
  414. m_sig_info->si_addr, m_sig_info->si_code );
  415. break;
  416. }
  417. break;
  418. case SIGSEGV:
  419. switch( m_sig_info->si_code ) {
  420. #ifndef BOOST_TEST_LIMITED_SIGNAL_DETAILS
  421. case SEGV_MAPERR:
  422. report_error( execution_exception::system_fatal_error,
  423. "memory access violation at address: 0x%08lx: no mapping at fault address",
  424. m_sig_info->si_addr );
  425. break;
  426. case SEGV_ACCERR:
  427. report_error( execution_exception::system_fatal_error,
  428. "memory access violation at address: 0x%08lx: invalid permissions",
  429. m_sig_info->si_addr );
  430. break;
  431. #endif
  432. default:
  433. report_error( execution_exception::system_fatal_error,
  434. "signal: SIGSEGV, si_code: %d (memory access violation at address: 0x%08lx)",
  435. m_sig_info->si_addr, m_sig_info->si_code );
  436. break;
  437. }
  438. break;
  439. case SIGBUS:
  440. switch( m_sig_info->si_code ) {
  441. #ifndef BOOST_TEST_LIMITED_SIGNAL_DETAILS
  442. case BUS_ADRALN:
  443. report_error( execution_exception::system_fatal_error,
  444. "memory access violation at address: 0x%08lx: invalid address alignment",
  445. m_sig_info->si_addr );
  446. break;
  447. case BUS_ADRERR:
  448. report_error( execution_exception::system_fatal_error,
  449. "memory access violation at address: 0x%08lx: non-existent physical address",
  450. m_sig_info->si_addr );
  451. break;
  452. case BUS_OBJERR:
  453. report_error( execution_exception::system_fatal_error,
  454. "memory access violation at address: 0x%08lx: object specific hardware error",
  455. m_sig_info->si_addr );
  456. break;
  457. #endif
  458. default:
  459. report_error( execution_exception::system_fatal_error,
  460. "signal: SIGSEGV, si_code: %d (memory access violation at address: 0x%08lx)",
  461. m_sig_info->si_addr, m_sig_info->si_code );
  462. break;
  463. }
  464. break;
  465. #if defined(BOOST_TEST_CATCH_SIGPOLL)
  466. case SIGPOLL:
  467. switch( m_sig_info->si_code ) {
  468. #ifndef BOOST_TEST_LIMITED_SIGNAL_DETAILS
  469. case POLL_IN:
  470. report_error( execution_exception::system_error,
  471. "data input available; band event %d",
  472. (int)m_sig_info->si_band );
  473. break;
  474. case POLL_OUT:
  475. report_error( execution_exception::system_error,
  476. "output buffers available; band event %d",
  477. (int)m_sig_info->si_band );
  478. break;
  479. case POLL_MSG:
  480. report_error( execution_exception::system_error,
  481. "input message available; band event %d",
  482. (int)m_sig_info->si_band );
  483. break;
  484. case POLL_ERR:
  485. report_error( execution_exception::system_error,
  486. "i/o error; band event %d",
  487. (int)m_sig_info->si_band );
  488. break;
  489. case POLL_PRI:
  490. report_error( execution_exception::system_error,
  491. "high priority input available; band event %d",
  492. (int)m_sig_info->si_band );
  493. break;
  494. #if defined(POLL_ERR) && defined(POLL_HUP) && (POLL_ERR - POLL_HUP)
  495. case POLL_HUP:
  496. report_error( execution_exception::system_error,
  497. "device disconnected; band event %d",
  498. (int)m_sig_info->si_band );
  499. break;
  500. #endif
  501. #endif
  502. default:
  503. report_error( execution_exception::system_error,
  504. "signal: SIGPOLL, si_code: %d (asynchronous I/O event occurred; band event %d)",
  505. (int)m_sig_info->si_band, m_sig_info->si_code );
  506. break;
  507. }
  508. break;
  509. #endif
  510. case SIGABRT:
  511. report_error( execution_exception::system_error,
  512. "signal: SIGABRT (application abort requested)" );
  513. break;
  514. case SIGALRM:
  515. report_error( execution_exception::timeout_error,
  516. "signal: SIGALRM (timeout while executing function)" );
  517. break;
  518. default:
  519. report_error( execution_exception::system_error,
  520. "unrecognized signal %d", m_sig_info->si_signo );
  521. }
  522. }
  523. //____________________________________________________________________________//
  524. // ************************************************************************** //
  525. // ************** boost::detail::signal_action ************** //
  526. // ************************************************************************** //
  527. // Forward declaration
  528. extern "C" {
  529. static void boost_execution_monitor_jumping_signal_handler( int sig, siginfo_t* info, void* context );
  530. static void boost_execution_monitor_attaching_signal_handler( int sig, siginfo_t* info, void* context );
  531. }
  532. class signal_action {
  533. typedef struct sigaction* sigaction_ptr;
  534. public:
  535. //Constructor
  536. signal_action();
  537. signal_action( int sig, bool install, bool attach_dbg, char* alt_stack );
  538. ~signal_action();
  539. private:
  540. // Data members
  541. int m_sig;
  542. bool m_installed;
  543. struct sigaction m_new_action;
  544. struct sigaction m_old_action;
  545. };
  546. //____________________________________________________________________________//
  547. signal_action::signal_action()
  548. : m_installed( false )
  549. {}
  550. //____________________________________________________________________________//
  551. signal_action::signal_action( int sig, bool install, bool attach_dbg, char* alt_stack )
  552. : m_sig( sig )
  553. , m_installed( install )
  554. {
  555. if( !install )
  556. return;
  557. std::memset( &m_new_action, 0, sizeof(struct sigaction) );
  558. BOOST_TEST_SYS_ASSERT( ::sigaction( m_sig , sigaction_ptr(), &m_new_action ) != -1 );
  559. if( m_new_action.sa_sigaction || m_new_action.sa_handler ) {
  560. m_installed = false;
  561. return;
  562. }
  563. m_new_action.sa_flags |= SA_SIGINFO;
  564. m_new_action.sa_sigaction = attach_dbg ? &boost_execution_monitor_attaching_signal_handler
  565. : &boost_execution_monitor_jumping_signal_handler;
  566. BOOST_TEST_SYS_ASSERT( sigemptyset( &m_new_action.sa_mask ) != -1 );
  567. #ifdef BOOST_TEST_USE_ALT_STACK
  568. if( alt_stack )
  569. m_new_action.sa_flags |= SA_ONSTACK;
  570. #endif
  571. BOOST_TEST_SYS_ASSERT( ::sigaction( m_sig, &m_new_action, &m_old_action ) != -1 );
  572. }
  573. //____________________________________________________________________________//
  574. signal_action::~signal_action()
  575. {
  576. if( m_installed )
  577. ::sigaction( m_sig, &m_old_action , sigaction_ptr() );
  578. }
  579. //____________________________________________________________________________//
  580. // ************************************************************************** //
  581. // ************** boost::detail::signal_handler ************** //
  582. // ************************************************************************** //
  583. class signal_handler {
  584. public:
  585. // Constructor
  586. explicit signal_handler( bool catch_system_errors, bool detect_fpe, unsigned timeout_microseconds, bool attach_dbg, char* alt_stack );
  587. // Destructor
  588. ~signal_handler();
  589. // access methods
  590. static sigjmp_buf& jump_buffer()
  591. {
  592. assert( !!s_active_handler );
  593. return s_active_handler->m_sigjmp_buf;
  594. }
  595. static system_signal_exception& sys_sig()
  596. {
  597. assert( !!s_active_handler );
  598. return s_active_handler->m_sys_sig;
  599. }
  600. private:
  601. // Data members
  602. signal_handler* m_prev_handler;
  603. unsigned m_timeout_microseconds;
  604. // Note: We intentionality do not catch SIGCHLD. Users have to deal with it themselves
  605. signal_action m_ILL_action;
  606. signal_action m_FPE_action;
  607. signal_action m_SEGV_action;
  608. signal_action m_BUS_action;
  609. signal_action m_CHLD_action;
  610. signal_action m_POLL_action;
  611. signal_action m_ABRT_action;
  612. signal_action m_ALRM_action;
  613. sigjmp_buf m_sigjmp_buf;
  614. system_signal_exception m_sys_sig;
  615. static signal_handler* s_active_handler;
  616. };
  617. // !! need to be placed in thread specific storage
  618. typedef signal_handler* signal_handler_ptr;
  619. signal_handler* signal_handler::s_active_handler = signal_handler_ptr();
  620. //____________________________________________________________________________//
  621. signal_handler::signal_handler( bool catch_system_errors, bool detect_fpe, unsigned timeout_microseconds, bool attach_dbg, char* alt_stack )
  622. : m_prev_handler( s_active_handler )
  623. , m_timeout_microseconds( timeout_microseconds )
  624. , m_ILL_action ( SIGILL , catch_system_errors, attach_dbg, alt_stack )
  625. , m_FPE_action ( SIGFPE , detect_fpe , attach_dbg, alt_stack )
  626. , m_SEGV_action( SIGSEGV, catch_system_errors, attach_dbg, alt_stack )
  627. , m_BUS_action ( SIGBUS , catch_system_errors, attach_dbg, alt_stack )
  628. #ifdef BOOST_TEST_CATCH_SIGPOLL
  629. , m_POLL_action( SIGPOLL, catch_system_errors, attach_dbg, alt_stack )
  630. #endif
  631. , m_ABRT_action( SIGABRT, catch_system_errors, attach_dbg, alt_stack )
  632. , m_ALRM_action( SIGALRM, timeout_microseconds > 0, attach_dbg, alt_stack )
  633. {
  634. s_active_handler = this;
  635. if( m_timeout_microseconds > 0 ) {
  636. ::alarm( 0 );
  637. ::alarm( static_cast<unsigned int>(std::ceil(timeout_microseconds / 1E6) )); // alarm has a precision to the seconds
  638. }
  639. #ifdef BOOST_TEST_USE_ALT_STACK
  640. if( alt_stack ) {
  641. stack_t sigstk;
  642. std::memset( &sigstk, 0, sizeof(stack_t) );
  643. BOOST_TEST_SYS_ASSERT( ::sigaltstack( 0, &sigstk ) != -1 );
  644. if( sigstk.ss_flags & SS_DISABLE ) {
  645. sigstk.ss_sp = alt_stack;
  646. sigstk.ss_size = BOOST_TEST_ALT_STACK_SIZE;
  647. sigstk.ss_flags = 0;
  648. BOOST_TEST_SYS_ASSERT( ::sigaltstack( &sigstk, 0 ) != -1 );
  649. }
  650. }
  651. #endif
  652. }
  653. //____________________________________________________________________________//
  654. signal_handler::~signal_handler()
  655. {
  656. assert( s_active_handler == this );
  657. if( m_timeout_microseconds > 0 )
  658. ::alarm( 0 );
  659. #ifdef BOOST_TEST_USE_ALT_STACK
  660. #ifdef __GNUC__
  661. // We shouldn't need to explicitly initialize all the members here,
  662. // but gcc warns if we don't, so add initializers for each of the
  663. // members specified in the POSIX std:
  664. stack_t sigstk = { 0, 0, 0 };
  665. #else
  666. stack_t sigstk = { };
  667. #endif
  668. sigstk.ss_size = MINSIGSTKSZ;
  669. sigstk.ss_flags = SS_DISABLE;
  670. if( ::sigaltstack( &sigstk, 0 ) == -1 ) {
  671. int error_n = errno;
  672. std::cerr << "******** errors disabling the alternate stack:" << std::endl
  673. << "\t#error:" << error_n << std::endl
  674. << "\t" << std::strerror( error_n ) << std::endl;
  675. }
  676. #endif
  677. s_active_handler = m_prev_handler;
  678. }
  679. //____________________________________________________________________________//
  680. // ************************************************************************** //
  681. // ************** execution_monitor_signal_handler ************** //
  682. // ************************************************************************** //
  683. extern "C" {
  684. static void boost_execution_monitor_jumping_signal_handler( int sig, siginfo_t* info, void* context )
  685. {
  686. signal_handler::sys_sig()( info, context );
  687. siglongjmp( signal_handler::jump_buffer(), sig );
  688. }
  689. //____________________________________________________________________________//
  690. static void boost_execution_monitor_attaching_signal_handler( int sig, siginfo_t* info, void* context )
  691. {
  692. if( !debug::attach_debugger( false ) )
  693. boost_execution_monitor_jumping_signal_handler( sig, info, context );
  694. // debugger attached; it will handle the signal
  695. BOOST_TEST_SYS_ASSERT( ::signal( sig, SIG_DFL ) != SIG_ERR );
  696. }
  697. //____________________________________________________________________________//
  698. }
  699. } // namespace detail
  700. // ************************************************************************** //
  701. // ************** execution_monitor::catch_signals ************** //
  702. // ************************************************************************** //
  703. int
  704. execution_monitor::catch_signals( boost::function<int ()> const& F )
  705. {
  706. using namespace detail;
  707. #if defined(__CYGWIN__)
  708. p_catch_system_errors.value = false;
  709. #endif
  710. #ifdef BOOST_TEST_USE_ALT_STACK
  711. if( !!p_use_alt_stack && !m_alt_stack )
  712. m_alt_stack.reset( new char[BOOST_TEST_ALT_STACK_SIZE] );
  713. #else
  714. p_use_alt_stack.value = false;
  715. #endif
  716. signal_handler local_signal_handler( p_catch_system_errors,
  717. p_catch_system_errors || (p_detect_fp_exceptions != fpe::BOOST_FPE_OFF),
  718. p_timeout,
  719. p_auto_start_dbg,
  720. !p_use_alt_stack ? 0 : m_alt_stack.get() );
  721. if( !sigsetjmp( signal_handler::jump_buffer(), 1 ) )
  722. return detail::do_invoke( m_custom_translators , F );
  723. else
  724. BOOST_TEST_I_THROW( local_signal_handler.sys_sig() );
  725. }
  726. //____________________________________________________________________________//
  727. #elif defined(BOOST_SEH_BASED_SIGNAL_HANDLING)
  728. // ************************************************************************** //
  729. // ************** Microsoft structured exception handling ************** //
  730. // ************************************************************************** //
  731. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0564))
  732. namespace { void _set_se_translator( void* ) {} }
  733. #endif
  734. namespace detail {
  735. // ************************************************************************** //
  736. // ************** boost::detail::system_signal_exception ************** //
  737. // ************************************************************************** //
  738. class system_signal_exception {
  739. public:
  740. // Constructor
  741. explicit system_signal_exception( execution_monitor* em )
  742. : m_em( em )
  743. , m_se_id( 0 )
  744. , m_fault_address( 0 )
  745. , m_dir( false )
  746. , m_timeout( false )
  747. {}
  748. void set_timed_out();
  749. void report() const;
  750. int operator()( unsigned id, _EXCEPTION_POINTERS* exps );
  751. private:
  752. // Data members
  753. execution_monitor* m_em;
  754. unsigned m_se_id;
  755. void* m_fault_address;
  756. bool m_dir;
  757. bool m_timeout;
  758. };
  759. //____________________________________________________________________________//
  760. #if BOOST_WORKAROUND( BOOST_MSVC, <= 1310)
  761. static void
  762. seh_catch_preventer( unsigned /* id */, _EXCEPTION_POINTERS* /* exps */ )
  763. {
  764. throw;
  765. }
  766. #endif
  767. //____________________________________________________________________________//
  768. void
  769. system_signal_exception::set_timed_out()
  770. {
  771. m_timeout = true;
  772. }
  773. //____________________________________________________________________________//
  774. int
  775. system_signal_exception::operator()( unsigned id, _EXCEPTION_POINTERS* exps )
  776. {
  777. const unsigned MSFT_CPP_EXCEPT = 0xE06d7363; // EMSC
  778. // C++ exception - allow to go through
  779. if( id == MSFT_CPP_EXCEPT )
  780. return EXCEPTION_CONTINUE_SEARCH;
  781. // FPE detection is enabled, while system exception detection is not - check if this is actually FPE
  782. if( !m_em->p_catch_system_errors ) {
  783. if( !m_em->p_detect_fp_exceptions )
  784. return EXCEPTION_CONTINUE_SEARCH;
  785. switch( id ) {
  786. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  787. case EXCEPTION_FLT_STACK_CHECK:
  788. case EXCEPTION_FLT_DENORMAL_OPERAND:
  789. case EXCEPTION_FLT_INEXACT_RESULT:
  790. case EXCEPTION_FLT_OVERFLOW:
  791. case EXCEPTION_FLT_UNDERFLOW:
  792. case EXCEPTION_FLT_INVALID_OPERATION:
  793. case STATUS_FLOAT_MULTIPLE_FAULTS:
  794. case STATUS_FLOAT_MULTIPLE_TRAPS:
  795. break;
  796. default:
  797. return EXCEPTION_CONTINUE_SEARCH;
  798. }
  799. }
  800. if( !!m_em->p_auto_start_dbg && debug::attach_debugger( false ) ) {
  801. m_em->p_catch_system_errors.value = false;
  802. #if BOOST_WORKAROUND( BOOST_MSVC, <= 1310)
  803. _set_se_translator( &seh_catch_preventer );
  804. #endif
  805. return EXCEPTION_CONTINUE_EXECUTION;
  806. }
  807. m_se_id = id;
  808. if( m_se_id == EXCEPTION_ACCESS_VIOLATION && exps->ExceptionRecord->NumberParameters == 2 ) {
  809. m_fault_address = (void*)exps->ExceptionRecord->ExceptionInformation[1];
  810. m_dir = exps->ExceptionRecord->ExceptionInformation[0] == 0;
  811. }
  812. return EXCEPTION_EXECUTE_HANDLER;
  813. }
  814. //____________________________________________________________________________//
  815. void
  816. system_signal_exception::report() const
  817. {
  818. switch( m_se_id ) {
  819. // cases classified as system_fatal_error
  820. case EXCEPTION_ACCESS_VIOLATION: {
  821. if( !m_fault_address )
  822. detail::report_error( execution_exception::system_fatal_error, "memory access violation" );
  823. else
  824. detail::report_error(
  825. execution_exception::system_fatal_error,
  826. "memory access violation occurred at address 0x%08lx, while attempting to %s",
  827. m_fault_address,
  828. m_dir ? " read inaccessible data"
  829. : " write to an inaccessible (or protected) address"
  830. );
  831. break;
  832. }
  833. case EXCEPTION_ILLEGAL_INSTRUCTION:
  834. detail::report_error( execution_exception::system_fatal_error, "illegal instruction" );
  835. break;
  836. case EXCEPTION_PRIV_INSTRUCTION:
  837. detail::report_error( execution_exception::system_fatal_error, "tried to execute an instruction whose operation is not allowed in the current machine mode" );
  838. break;
  839. case EXCEPTION_IN_PAGE_ERROR:
  840. detail::report_error( execution_exception::system_fatal_error, "access to a memory page that is not present" );
  841. break;
  842. case EXCEPTION_STACK_OVERFLOW:
  843. detail::report_error( execution_exception::system_fatal_error, "stack overflow" );
  844. break;
  845. case EXCEPTION_NONCONTINUABLE_EXCEPTION:
  846. detail::report_error( execution_exception::system_fatal_error, "tried to continue execution after a non continuable exception occurred" );
  847. break;
  848. // cases classified as (non-fatal) system_trap
  849. case EXCEPTION_DATATYPE_MISALIGNMENT:
  850. detail::report_error( execution_exception::system_error, "data misalignment" );
  851. break;
  852. case EXCEPTION_INT_DIVIDE_BY_ZERO:
  853. detail::report_error( execution_exception::system_error, "integer divide by zero" );
  854. break;
  855. case EXCEPTION_INT_OVERFLOW:
  856. detail::report_error( execution_exception::system_error, "integer overflow" );
  857. break;
  858. case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  859. detail::report_error( execution_exception::system_error, "array bounds exceeded" );
  860. break;
  861. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  862. detail::report_error( execution_exception::system_error, "floating point divide by zero" );
  863. break;
  864. case EXCEPTION_FLT_STACK_CHECK:
  865. detail::report_error( execution_exception::system_error,
  866. "stack overflowed or underflowed as the result of a floating-point operation" );
  867. break;
  868. case EXCEPTION_FLT_DENORMAL_OPERAND:
  869. detail::report_error( execution_exception::system_error,
  870. "operand of floating point operation is denormal" );
  871. break;
  872. case EXCEPTION_FLT_INEXACT_RESULT:
  873. detail::report_error( execution_exception::system_error,
  874. "result of a floating-point operation cannot be represented exactly" );
  875. break;
  876. case EXCEPTION_FLT_OVERFLOW:
  877. detail::report_error( execution_exception::system_error,
  878. "exponent of a floating-point operation is greater than the magnitude allowed by the corresponding type" );
  879. break;
  880. case EXCEPTION_FLT_UNDERFLOW:
  881. detail::report_error( execution_exception::system_error,
  882. "exponent of a floating-point operation is less than the magnitude allowed by the corresponding type" );
  883. break;
  884. case EXCEPTION_FLT_INVALID_OPERATION:
  885. detail::report_error( execution_exception::system_error, "floating point error" );
  886. break;
  887. case STATUS_FLOAT_MULTIPLE_FAULTS:
  888. detail::report_error( execution_exception::system_error, "multiple floating point errors" );
  889. break;
  890. case STATUS_FLOAT_MULTIPLE_TRAPS:
  891. detail::report_error( execution_exception::system_error, "multiple floating point errors" );
  892. break;
  893. case EXCEPTION_BREAKPOINT:
  894. detail::report_error( execution_exception::system_error, "breakpoint encountered" );
  895. break;
  896. default:
  897. if( m_timeout ) {
  898. detail::report_error(execution_exception::timeout_error, "timeout while executing function");
  899. }
  900. else {
  901. detail::report_error( execution_exception::system_error, "unrecognized exception. Id: 0x%08lx", m_se_id );
  902. }
  903. break;
  904. }
  905. }
  906. //____________________________________________________________________________//
  907. // ************************************************************************** //
  908. // ************** assert_reporting_function ************** //
  909. // ************************************************************************** //
  910. int BOOST_TEST_CALL_DECL
  911. assert_reporting_function( int reportType, char* userMessage, int* )
  912. {
  913. // write this way instead of switch to avoid unreachable statements
  914. if( reportType == BOOST_TEST_CRT_ASSERT || reportType == BOOST_TEST_CRT_ERROR )
  915. detail::report_error( reportType == BOOST_TEST_CRT_ASSERT ? execution_exception::user_error : execution_exception::system_error, userMessage );
  916. return 0;
  917. } // assert_reporting_function
  918. //____________________________________________________________________________//
  919. void BOOST_TEST_CALL_DECL
  920. invalid_param_handler( wchar_t const* /* expr */,
  921. wchar_t const* /* func */,
  922. wchar_t const* /* file */,
  923. unsigned /* line */,
  924. uintptr_t /* reserved */)
  925. {
  926. detail::report_error( execution_exception::user_error,
  927. "Invalid parameter detected by C runtime library" );
  928. }
  929. //____________________________________________________________________________//
  930. } // namespace detail
  931. // ************************************************************************** //
  932. // ************** execution_monitor::catch_signals ************** //
  933. // ************************************************************************** //
  934. int
  935. execution_monitor::catch_signals( boost::function<int ()> const& F )
  936. {
  937. _invalid_parameter_handler old_iph = _invalid_parameter_handler();
  938. BOOST_TEST_CRT_HOOK_TYPE old_crt_hook = 0;
  939. if( p_catch_system_errors ) {
  940. old_crt_hook = BOOST_TEST_CRT_SET_HOOK( &detail::assert_reporting_function );
  941. old_iph = _set_invalid_parameter_handler(
  942. reinterpret_cast<_invalid_parameter_handler>( &detail::invalid_param_handler ) );
  943. } else if( !p_detect_fp_exceptions ) {
  944. #if BOOST_WORKAROUND( BOOST_MSVC, <= 1310)
  945. _set_se_translator( &detail::seh_catch_preventer );
  946. #endif
  947. }
  948. #if defined(BOOST_TEST_WIN32_WAITABLE_TIMERS)
  949. HANDLE htimer = INVALID_HANDLE_VALUE;
  950. BOOL bTimerSuccess = FALSE;
  951. if( p_timeout ) {
  952. htimer = ::CreateWaitableTimer(
  953. NULL,
  954. TRUE,
  955. TEXT("Boost.Test timer"));
  956. if( htimer != INVALID_HANDLE_VALUE ) {
  957. LARGE_INTEGER liDueTime;
  958. liDueTime.QuadPart = - static_cast<signed long int>(p_timeout) * 10; // resolution of 100 ns
  959. bTimerSuccess = ::SetWaitableTimer(
  960. htimer,
  961. &liDueTime,
  962. 0,
  963. 0,
  964. 0,
  965. FALSE); // Do not restore a suspended system
  966. }
  967. }
  968. #endif
  969. detail::system_signal_exception SSE( this );
  970. int ret_val = 0;
  971. // clang windows workaround: this not available in __finally scope
  972. bool l_catch_system_errors = p_catch_system_errors;
  973. __try {
  974. __try {
  975. ret_val = detail::do_invoke( m_custom_translators, F );
  976. }
  977. __except( SSE( GetExceptionCode(), GetExceptionInformation() ) ) {
  978. throw SSE;
  979. }
  980. // we check for time outs: we do not have any signaling facility on Win32
  981. // however, we signal a timeout as a hard error as for the other operating systems
  982. // and throw the signal error handler
  983. if( bTimerSuccess && htimer != INVALID_HANDLE_VALUE) {
  984. if (::WaitForSingleObject(htimer, 0) == WAIT_OBJECT_0) {
  985. SSE.set_timed_out();
  986. throw SSE;
  987. }
  988. }
  989. }
  990. __finally {
  991. #if defined(BOOST_TEST_WIN32_WAITABLE_TIMERS)
  992. if( htimer != INVALID_HANDLE_VALUE ) {
  993. ::CloseHandle(htimer);
  994. }
  995. #endif
  996. if( l_catch_system_errors ) {
  997. BOOST_TEST_CRT_SET_HOOK( old_crt_hook );
  998. _set_invalid_parameter_handler( old_iph );
  999. }
  1000. }
  1001. return ret_val;
  1002. }
  1003. //____________________________________________________________________________//
  1004. #else // default signal handler
  1005. namespace detail {
  1006. class system_signal_exception {
  1007. public:
  1008. void report() const {}
  1009. };
  1010. } // namespace detail
  1011. int
  1012. execution_monitor::catch_signals( boost::function<int ()> const& F )
  1013. {
  1014. return detail::do_invoke( m_custom_translators , F );
  1015. }
  1016. //____________________________________________________________________________//
  1017. #endif // choose signal handler
  1018. // ************************************************************************** //
  1019. // ************** execution_monitor ************** //
  1020. // ************************************************************************** //
  1021. execution_monitor::execution_monitor()
  1022. : p_catch_system_errors( true )
  1023. , p_auto_start_dbg( false )
  1024. , p_timeout( 0 )
  1025. , p_use_alt_stack( true )
  1026. , p_detect_fp_exceptions( fpe::BOOST_FPE_OFF )
  1027. {}
  1028. //____________________________________________________________________________//
  1029. int
  1030. execution_monitor::execute( boost::function<int ()> const& F )
  1031. {
  1032. if( debug::under_debugger() )
  1033. p_catch_system_errors.value = false;
  1034. BOOST_TEST_I_TRY {
  1035. detail::fpe_except_guard G( p_detect_fp_exceptions );
  1036. boost::ignore_unused( G );
  1037. return catch_signals( F );
  1038. }
  1039. #ifndef BOOST_NO_EXCEPTIONS
  1040. // Catch-clause reference arguments are a bit different from function
  1041. // arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't
  1042. // required. Programmers ask for const anyhow, so we supply it. That's
  1043. // easier than answering questions about non-const usage.
  1044. catch( char const* ex )
  1045. { detail::report_error( execution_exception::cpp_exception_error,
  1046. "C string: %s", ex ); }
  1047. catch( std::string const& ex )
  1048. { detail::report_error( execution_exception::cpp_exception_error,
  1049. "std::string: %s", ex.c_str() ); }
  1050. // boost::exception (before std::exception, with extended diagnostic)
  1051. catch( boost::exception const& ex )
  1052. { detail::report_error( execution_exception::cpp_exception_error,
  1053. &ex,
  1054. #if defined(BOOST_NO_TYPEID) || defined(BOOST_NO_RTTI)
  1055. "unknown boost::exception" ); }
  1056. #else
  1057. boost::diagnostic_information(ex).c_str() ); }
  1058. #endif
  1059. // std:: exceptions
  1060. #if defined(BOOST_NO_TYPEID) || defined(BOOST_NO_RTTI)
  1061. #define CATCH_AND_REPORT_STD_EXCEPTION( ex_name ) \
  1062. catch( ex_name const& ex ) \
  1063. { detail::report_error( execution_exception::cpp_exception_error, \
  1064. current_exception_cast<boost::exception const>(), \
  1065. #ex_name ": %s", ex.what() ); } \
  1066. /**/
  1067. #else
  1068. #define CATCH_AND_REPORT_STD_EXCEPTION( ex_name ) \
  1069. catch( ex_name const& ex ) \
  1070. { detail::report_error( execution_exception::cpp_exception_error, \
  1071. current_exception_cast<boost::exception const>(), \
  1072. "%s: %s", detail::typeid_name(ex).c_str(), ex.what() ); } \
  1073. /**/
  1074. #endif
  1075. CATCH_AND_REPORT_STD_EXCEPTION( std::bad_alloc )
  1076. CATCH_AND_REPORT_STD_EXCEPTION( std::bad_cast )
  1077. CATCH_AND_REPORT_STD_EXCEPTION( std::bad_typeid )
  1078. CATCH_AND_REPORT_STD_EXCEPTION( std::bad_exception )
  1079. CATCH_AND_REPORT_STD_EXCEPTION( std::domain_error )
  1080. CATCH_AND_REPORT_STD_EXCEPTION( std::invalid_argument )
  1081. CATCH_AND_REPORT_STD_EXCEPTION( std::length_error )
  1082. CATCH_AND_REPORT_STD_EXCEPTION( std::out_of_range )
  1083. CATCH_AND_REPORT_STD_EXCEPTION( std::range_error )
  1084. CATCH_AND_REPORT_STD_EXCEPTION( std::overflow_error )
  1085. CATCH_AND_REPORT_STD_EXCEPTION( std::underflow_error )
  1086. CATCH_AND_REPORT_STD_EXCEPTION( std::logic_error )
  1087. CATCH_AND_REPORT_STD_EXCEPTION( std::runtime_error )
  1088. CATCH_AND_REPORT_STD_EXCEPTION( std::exception )
  1089. #undef CATCH_AND_REPORT_STD_EXCEPTION
  1090. // system errors
  1091. catch( system_error const& ex )
  1092. { detail::report_error( execution_exception::cpp_exception_error,
  1093. "system_error produced by: %s: %s", ex.p_failed_exp, std::strerror( ex.p_errno ) ); }
  1094. catch( detail::system_signal_exception const& ex )
  1095. { ex.report(); }
  1096. // not an error
  1097. catch( execution_aborted const& )
  1098. { return 0; }
  1099. // just forward
  1100. catch( execution_exception const& )
  1101. { throw; }
  1102. // unknown error
  1103. catch( ... )
  1104. { detail::report_error( execution_exception::cpp_exception_error, "unknown type" ); }
  1105. #endif // !BOOST_NO_EXCEPTIONS
  1106. return 0; // never reached; supplied to quiet compiler warnings
  1107. } // execute
  1108. //____________________________________________________________________________//
  1109. namespace detail {
  1110. struct forward {
  1111. explicit forward( boost::function<void ()> const& F ) : m_F( F ) {}
  1112. int operator()() { m_F(); return 0; }
  1113. boost::function<void ()> const& m_F;
  1114. };
  1115. } // namespace detail
  1116. void
  1117. execution_monitor::vexecute( boost::function<void ()> const& F )
  1118. {
  1119. execute( detail::forward( F ) );
  1120. }
  1121. // ************************************************************************** //
  1122. // ************** system_error ************** //
  1123. // ************************************************************************** //
  1124. system_error::system_error( char const* exp )
  1125. #ifdef UNDER_CE
  1126. : p_errno( GetLastError() )
  1127. #else
  1128. : p_errno( errno )
  1129. #endif
  1130. , p_failed_exp( exp )
  1131. {}
  1132. //____________________________________________________________________________//
  1133. // ************************************************************************** //
  1134. // ************** execution_exception ************** //
  1135. // ************************************************************************** //
  1136. execution_exception::execution_exception( error_code ec_, const_string what_msg_, location const& location_ )
  1137. : m_error_code( ec_ )
  1138. , m_what( what_msg_.empty() ? BOOST_TEST_L( "uncaught exception, system error or abort requested" ) : what_msg_ )
  1139. , m_location( location_ )
  1140. {}
  1141. //____________________________________________________________________________//
  1142. execution_exception::location::location( char const* file_name, size_t line_num, char const* func )
  1143. : m_file_name( file_name ? file_name : "unknown location" )
  1144. , m_line_num( line_num )
  1145. , m_function( func )
  1146. {}
  1147. execution_exception::location::location(const_string file_name, size_t line_num, char const* func )
  1148. : m_file_name( file_name )
  1149. , m_line_num( line_num )
  1150. , m_function( func )
  1151. {}
  1152. //____________________________________________________________________________//
  1153. // ************************************************************************** //
  1154. // **************Floating point exception management interface ************** //
  1155. // ************************************************************************** //
  1156. namespace fpe {
  1157. unsigned
  1158. enable( unsigned mask )
  1159. {
  1160. boost::ignore_unused(mask);
  1161. #if defined(BOOST_TEST_FPE_SUPPORT_WITH_SEH__)
  1162. _clearfp();
  1163. #if BOOST_WORKAROUND( BOOST_MSVC, <= 1310)
  1164. unsigned old_cw = ::_controlfp( 0, 0 );
  1165. ::_controlfp( old_cw & ~mask, BOOST_FPE_ALL );
  1166. #else
  1167. unsigned old_cw;
  1168. if( ::_controlfp_s( &old_cw, 0, 0 ) != 0 )
  1169. return BOOST_FPE_INV;
  1170. // Set the control word
  1171. if( ::_controlfp_s( 0, old_cw & ~mask, BOOST_FPE_ALL ) != 0 )
  1172. return BOOST_FPE_INV;
  1173. #endif
  1174. return ~old_cw & BOOST_FPE_ALL;
  1175. #elif defined(BOOST_TEST_FPE_SUPPORT_WITH_GLIBC_EXTENSIONS__)
  1176. // same macro definition as in execution_monitor.hpp
  1177. if (BOOST_FPE_ALL == BOOST_FPE_OFF)
  1178. /* Not Implemented */
  1179. return BOOST_FPE_OFF;
  1180. feclearexcept(BOOST_FPE_ALL);
  1181. int res = feenableexcept( mask );
  1182. return res == -1 ? (unsigned)BOOST_FPE_INV : (unsigned)res;
  1183. #else
  1184. /* Not Implemented */
  1185. return BOOST_FPE_OFF;
  1186. #endif
  1187. }
  1188. //____________________________________________________________________________//
  1189. unsigned
  1190. disable( unsigned mask )
  1191. {
  1192. boost::ignore_unused(mask);
  1193. #if defined(BOOST_TEST_FPE_SUPPORT_WITH_SEH__)
  1194. _clearfp();
  1195. #if BOOST_WORKAROUND( BOOST_MSVC, <= 1310)
  1196. unsigned old_cw = ::_controlfp( 0, 0 );
  1197. ::_controlfp( old_cw | mask, BOOST_FPE_ALL );
  1198. #else
  1199. unsigned old_cw;
  1200. if( ::_controlfp_s( &old_cw, 0, 0 ) != 0 )
  1201. return BOOST_FPE_INV;
  1202. // Set the control word
  1203. if( ::_controlfp_s( 0, old_cw | mask, BOOST_FPE_ALL ) != 0 )
  1204. return BOOST_FPE_INV;
  1205. #endif
  1206. return ~old_cw & BOOST_FPE_ALL;
  1207. #elif defined(BOOST_TEST_FPE_SUPPORT_WITH_GLIBC_EXTENSIONS__)
  1208. if (BOOST_FPE_ALL == BOOST_FPE_OFF)
  1209. /* Not Implemented */
  1210. return BOOST_FPE_INV;
  1211. feclearexcept(BOOST_FPE_ALL);
  1212. int res = fedisableexcept( mask );
  1213. return res == -1 ? (unsigned)BOOST_FPE_INV : (unsigned)res;
  1214. #else
  1215. /* Not Implemented */
  1216. return BOOST_FPE_INV;
  1217. #endif
  1218. }
  1219. //____________________________________________________________________________//
  1220. } // namespace fpe
  1221. } // namespace boost
  1222. #include <boost/test/detail/enable_warnings.hpp>
  1223. #endif // BOOST_TEST_EXECUTION_MONITOR_IPP_012205GER