b0119c0646e79da54d8882559ff6acd01b0299c6
[folly.git] / folly / detail / UncaughtExceptionCounter.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef FOLLY_DETAIL_UNCAUGHTEXCEPTIONCOUNTER_H_
18 #define FOLLY_DETAIL_UNCAUGHTEXCEPTIONCOUNTER_H_
19
20 #include <exception>
21
22 #if defined(__GNUG__) || defined(__CLANG__)
23 #define FOLLY_EXCEPTION_COUNT_USE_CXA_GET_GLOBALS
24 namespace __cxxabiv1 {
25 // forward declaration (originally defined in unwind-cxx.h from from libstdc++)
26 struct __cxa_eh_globals;
27 // declared in cxxabi.h from libstdc++-v3
28 extern "C" __cxa_eh_globals* __cxa_get_globals() noexcept;
29 }
30 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) && \
31     (_MSC_VER < 1900) // MSVC++ 8.0 or greater
32 #define FOLLY_EXCEPTION_COUNT_USE_GETPTD
33 // forward declaration (originally defined in mtdll.h from MSVCRT)
34 struct _tiddata;
35 extern "C" _tiddata* _getptd(); // declared in mtdll.h from MSVCRT
36 #elif defined(_MSC_VER) && (_MSC_VER >= 1900) // MSVC++ 2015
37 #define FOLLY_EXCEPTION_COUNT_USE_STD
38 #else
39 // Raise an error when trying to use this on unsupported platforms.
40 #error "Unsupported platform, don't include this header."
41 #endif
42
43
44 namespace folly { namespace detail {
45
46 /**
47  * Used to check if a new uncaught exception was thrown by monitoring the
48  * number of uncaught exceptions.
49  *
50  * Usage:
51  *  - create a new UncaughtExceptionCounter object
52  *  - call isNewUncaughtException() on the new object to check if a new
53  *    uncaught exception was thrown since the object was created
54  */
55 class UncaughtExceptionCounter {
56  public:
57   UncaughtExceptionCounter() noexcept
58       : exceptionCount_(getUncaughtExceptionCount()) {}
59
60   UncaughtExceptionCounter(const UncaughtExceptionCounter& other) noexcept
61       : exceptionCount_(other.exceptionCount_) {}
62
63   bool isNewUncaughtException() noexcept {
64     return getUncaughtExceptionCount() > exceptionCount_;
65   }
66
67  private:
68   int getUncaughtExceptionCount() noexcept;
69
70   int exceptionCount_;
71 };
72
73 /**
74  * Returns the number of uncaught exceptions.
75  *
76  * This function is based on Evgeny Panasyuk's implementation from here:
77  * http://fburl.com/15190026
78  */
79 inline int UncaughtExceptionCounter::getUncaughtExceptionCount() noexcept {
80 #if defined(FOLLY_EXCEPTION_COUNT_USE_CXA_GET_GLOBALS)
81   // __cxa_get_globals returns a __cxa_eh_globals* (defined in unwind-cxx.h).
82   // The offset below returns __cxa_eh_globals::uncaughtExceptions.
83   return *(reinterpret_cast<unsigned int*>(static_cast<char*>(
84       static_cast<void*>(__cxxabiv1::__cxa_get_globals())) + sizeof(void*)));
85 #elif defined(FOLLY_EXCEPTION_COUNT_USE_GETPTD)
86   // _getptd() returns a _tiddata* (defined in mtdll.h).
87   // The offset below returns _tiddata::_ProcessingThrow.
88   return *(reinterpret_cast<int*>(static_cast<char*>(
89       static_cast<void*>(_getptd())) + sizeof(void*) * 28 + 0x4 * 8));
90 #elif defined(FOLLY_EXCEPTION_COUNT_USE_STD)
91   return std::uncaught_exceptions();
92 #endif
93 }
94
95 }} // namespaces
96
97 #endif /* FOLLY_DETAIL_UNCAUGHTEXCEPTIONCOUNTER_H_ */