]> git.sesse.net Git - casparcg/blob - common/exception/win32_exception.cpp
* Created custom decklink allocator for reducing memory footprint.
[casparcg] / common / exception / win32_exception.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../stdafx.h"\r
23 \r
24 #include <boost/thread.hpp>\r
25 \r
26 #include "win32_exception.h"\r
27 \r
28 #include "../log/log.h"\r
29 #include "../concurrency/executor.h"\r
30 \r
31 namespace caspar {\r
32 \r
33 bool& installed_for_thread()\r
34 {\r
35         static boost::thread_specific_ptr<bool> installed;\r
36 \r
37         auto for_thread = installed.get();\r
38         \r
39         if (!for_thread)\r
40         {\r
41                 for_thread = new bool(false);\r
42                 installed.reset(for_thread);\r
43         }\r
44 \r
45         return *for_thread;\r
46 }\r
47 \r
48 void win32_exception::install_handler() \r
49 {\r
50 //#ifndef _DEBUG\r
51         _set_se_translator(win32_exception::Handler);\r
52         installed_for_thread() = true;\r
53 //#endif\r
54 }\r
55 \r
56 void win32_exception::ensure_handler_installed_for_thread(\r
57                 const char* thread_description)\r
58 {\r
59         if (!installed_for_thread())\r
60         {\r
61                 install_handler();\r
62 \r
63                 if (thread_description)\r
64                         detail::SetThreadName(GetCurrentThreadId(), thread_description);\r
65         }\r
66 }\r
67 \r
68 void win32_exception::Handler(unsigned int errorCode, EXCEPTION_POINTERS* pInfo) {\r
69         switch(errorCode)\r
70         {\r
71         case EXCEPTION_ACCESS_VIOLATION:\r
72                 throw win32_access_violation(*(pInfo->ExceptionRecord));\r
73                 break;\r
74 \r
75         default:\r
76                 throw win32_exception(*(pInfo->ExceptionRecord));\r
77         }\r
78 }\r
79 \r
80 win32_exception::win32_exception(const EXCEPTION_RECORD& info) : message_("Win32 exception"), location_(info.ExceptionAddress), errorCode_(info.ExceptionCode)\r
81 {\r
82         switch(info.ExceptionCode)\r
83         {\r
84         case EXCEPTION_ACCESS_VIOLATION:\r
85                 message_ = "Access violation";\r
86                 break;\r
87         case EXCEPTION_FLT_DIVIDE_BY_ZERO:\r
88         case EXCEPTION_INT_DIVIDE_BY_ZERO:\r
89                 message_ = "Divide by zero";\r
90                 break;\r
91         }\r
92 }\r
93 \r
94 win32_access_violation::win32_access_violation(const EXCEPTION_RECORD& info) : win32_exception(info), isWrite_(false), badAddress_(0) \r
95 {\r
96         isWrite_ = info.ExceptionInformation[0] == 1;\r
97         badAddress_ = reinterpret_cast<win32_exception::address>(info.ExceptionInformation[1]);\r
98 }\r
99 \r
100 const char* win32_access_violation::what() const\r
101 {\r
102         sprintf_s<>(messageBuffer_, "Access violation at %p, trying to %s %p", location(), isWrite_?"write":"read", badAddress_);\r
103 \r
104         return messageBuffer_;\r
105 }\r
106 \r
107 }