| 1 | |
| 2 | // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, |
| 6 | // are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions, and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | // this list of conditions, and the following disclaimer in the documentation |
| 13 | // and/or other materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the names of the copyright holders nor the names of their contributors |
| 16 | // may be used to endorse or promote products derived from this software without |
| 17 | // specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 | // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR |
| 26 | // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS |
| 28 | // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES |
| 29 | // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE |
| 30 | // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES. |
| 31 | // |
| 32 | |
| 33 | #include "journalstackwalker.h" |
| 34 | #include "journalexception.h" |
| 35 | |
| 36 | /*! \brief A helper class that combines the StackWalker output into a std::string */ |
| 37 | class JournalExceptionStackWalker : public StackWalker { |
| 38 | public: |
| 39 | JournalExceptionStackWalker(JournalException* exc) |
| 40 | : StackWalker() |
| 41 | , m_exception(exc) |
| 42 | {} |
| 43 | |
| 44 | JournalException* m_exception; |
| 45 | |
| 46 | virtual void OnOutput(LPCSTR szText) |
| 47 | { |
| 48 | m_exception->m_stack += szText; |
| 49 | m_exception->m_stack += "\n"; |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | /*! \class JournalException |
| 54 | \brief An exception class that automatically includes stack dump information |
| 55 | */ |
| 56 | |
| 57 | /*! \brief Constructor, copies the \a message and creates a stack dump */ |
| 58 | JournalException::JournalException(std::string const& message) |
| 59 | : XsException(XRV_ERROR, message) |
| 60 | , m_stack("************ Dump Begin ************\n") |
| 61 | { |
| 62 | JournalExceptionStackWalker sw(this); |
| 63 | sw.ShowCallstack(); |
| 64 | m_stack += "************* Dump End *************"; |
| 65 | } |
| 66 | |
| 67 | /*! \brief The message as supplied to the constructor */ |
| 68 | const char* JournalException::msg() const |
| 69 | { |
| 70 | return what(); |
| 71 | } |
| 72 | |
| 73 | /*! \brief The stack dump as it was at the time of object construction */ |
| 74 | const std::string& JournalException::stack() const |
| 75 | { |
| 76 | return m_stack; |
| 77 | } |
| 78 | |
| 79 | #ifdef XSENS_DEBUG |
| 80 | #define GOTODEBDEF true |
| 81 | #else |
| 82 | #define GOTODEBDEF false |
| 83 | #endif |
| 84 | |
| 85 | /*! When set to true, any caught exception at the C level will be passed on upward (ie to the debugger) |
| 86 | When set to false, the exception handling stops at this level. This only applies to C level exceptions. |
| 87 | */ |
| 88 | bool gOnExceptionGotoDebugger = GOTODEBDEF; |
| 89 | |
| 90 | #ifdef XSENS_WINDOWS |
| 91 | /*! \brief Exception filter for crash handler. |
| 92 | |
| 93 | Using this filter in __try __except() will generate a crash log with a call stack. |
| 94 | \param pExPtrs pointers to exception struct, use GetExceptionInformation() |
| 95 | \param journal The Journaller object to use for logging |
| 96 | */ |
| 97 | LONG journallerExceptionFilter(EXCEPTION_POINTERS* pExPtrs, Journaller* journal) |
| 98 | { |
| 99 | journal->log(JLL_Fatal, "******* C level exception (CRASH) *******"); |
| 100 | journal->log(JLL_Fatal, "************ Dump Begin ************"); |
| 101 | JournalStackWalker sw(journal); |
| 102 | sw.ShowCallstack(GetCurrentThread(), pExPtrs->ContextRecord); |
| 103 | journal->log(JLL_Fatal, "************* Dump End *************"); |
| 104 | |
| 105 | return gOnExceptionGotoDebugger ? EXCEPTION_CONTINUE_SEARCH : EXCEPTION_EXECUTE_HANDLER; |
| 106 | } |
| 107 | #endif |