343b8be9ee4325e86506f28f1610f70027f41f9b
[folly.git] / folly / detail / UncaughtExceptionCounter.h
1 /*
2  * Copyright 2014 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) // MSVC++ 8.0 or greater
31 #define FOLLY_EXCEPTION_COUNT_USE_GETPTD
32 // forward declaration (originally defined in mtdll.h from MSVCRT)
33 struct _tiddata;
34 extern "C" _tiddata* _getptd(); // declared in mtdll.h from MSVCRT
35 #else
36 // Raise an error when trying to use this on unsupported platforms.
37 #error "Unsupported platform, don't include this header."
38 #endif
39
40
41 namespace folly { namespace detail {
42
43 /**
44  * Used to check if a new uncaught exception was thrown by monitoring the
45  * number of uncaught exceptions.
46  *
47  * Usage:
48  *  - create a new UncaughtExceptionCounter object
49  *  - call isNewUncaughtException() on the new object to check if a new
50  *    uncaught exception was thrown since the object was created
51  */
52 class UncaughtExceptionCounter {
53  public:
54   UncaughtExceptionCounter()
55     : exceptionCount_(getUncaughtExceptionCount()) {}
56
57   UncaughtExceptionCounter(const UncaughtExceptionCounter& other)
58     : exceptionCount_(other.exceptionCount_) {}
59
60   bool isNewUncaughtException() noexcept {
61     return getUncaughtExceptionCount() > exceptionCount_;
62   }
63
64  private:
65   int getUncaughtExceptionCount() noexcept;
66
67   int exceptionCount_;
68 };
69
70 /**
71  * Returns the number of uncaught exceptions.
72  *
73  * This function is based on Evgeny Panasyuk's implementation from here:
74  * http://fburl.com/15190026
75  */
76 inline int UncaughtExceptionCounter::getUncaughtExceptionCount() noexcept {
77 #if defined(FOLLY_EXCEPTION_COUNT_USE_CXA_GET_GLOBALS)
78   // __cxa_get_globals returns a __cxa_eh_globals* (defined in unwind-cxx.h).
79   // The offset below returns __cxa_eh_globals::uncaughtExceptions.
80   return *(reinterpret_cast<unsigned int*>(static_cast<char*>(
81       static_cast<void*>(__cxxabiv1::__cxa_get_globals())) + sizeof(void*)));
82 #elif defined(FOLLY_EXCEPTION_COUNT_USE_GETPTD)
83   // _getptd() returns a _tiddata* (defined in mtdll.h).
84   // The offset below returns _tiddata::_ProcessingThrow.
85   return *(reinterpret_cast<int*>(static_cast<char*>(
86       static_cast<void*>(_getptd())) + sizeof(void*) * 28 + 0x4 * 8));
87 #endif
88 }
89
90 }} // namespaces
91
92 #endif /* FOLLY_DETAIL_UNCAUGHTEXCEPTIONCOUNTER_H_ */