Call post push hook even if push wasn't successful
[TortoiseGit.git] / src / Utils / CrashReport.h
blobb29c76468ce15649be738a358a86030376c5c480
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013 - TortoiseGit
4 // Copyright (C) 2012 - 2013 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #include "../../ext/CrashServer/CrashHandler/CrashHandler/CrashHandler.h"
22 #include <time.h>
23 #include <string>
24 #include <tchar.h>
26 // dummy define, needed only when we use crashrpt instead of this.
27 #define CR_AF_MAKE_FILE_COPY 0
29 /**
30 * \ingroup Utils
31 * helper class for the CrashServerSDK
33 class CCrashReport
35 private:
36 CCrashReport(void)
37 : m_InitCrashHandler(nullptr)
38 , m_SendReport(nullptr)
39 , m_IsReadyToExit(nullptr)
40 , m_AddUserInfoToReport(nullptr)
41 , m_AddFileToReport(nullptr)
42 , m_RemoveFileFromReport(nullptr)
43 , m_GetVersionFromApp(nullptr)
44 , m_GetVersionFromFile(nullptr)
46 LoadDll();
49 ~CCrashReport(void)
53 public:
54 static CCrashReport& Instance()
56 static CCrashReport instance;
57 return instance;
60 int Uninstall(void) { return FALSE; }
61 int AddFile2(LPCTSTR pszFile,LPCTSTR pszDestFile,LPCTSTR /*pszDesc*/,DWORD /*dwFlags*/)
63 return AddFileToReport(pszFile, pszDestFile) ? 1 : 0;
67 //! Initializes crash handler.
68 //! \note You may call this function multiple times if some data has changed.
69 //! \return Return \b true if crash handling was enabled.
70 bool InitCrashHandler(
71 ApplicationInfo* applicationInfo, //!< [in] Pointer to the ApplicationInfo structure that identifies your application.
72 HandlerSettings* handlerSettings, //!< [in] Pointer to the HandlerSettings structure that customizes crash handling behavior. This paramenter can be \b NULL.
73 BOOL ownProcess = TRUE //!< [in] If you own the process your code running in set this option to \b TRUE. If don't (for example you write
74 //!< a plugin to some external application) set this option to \b FALSE. In that case you need to explicitly
75 //!< catch exceptions. See \ref SendReport for more information.
76 ) throw()
78 if (!m_InitCrashHandler)
79 return false;
81 return m_InitCrashHandler(applicationInfo, handlerSettings, ownProcess) != FALSE;
84 //! You may add any key/value pair to crash report.
85 //! \return If the function succeeds, the return value is \b true.
86 bool AddUserInfoToReport(
87 LPCWSTR key, //!< [in] key string that will be added to the report.
88 LPCWSTR value //!< [in] value for the key.
89 ) throw()
91 if (!m_AddUserInfoToReport)
92 return false;
93 m_AddUserInfoToReport(key, value);
94 return true;
97 //! You may add any file to crash report. This file will be read when crash appears and will be sent within the report.
98 //! Multiple files may be added. Filename of the file in the report may be changed to any name.
99 //! \return If the function succeeds, the return value is \b true.
100 bool AddFileToReport(
101 LPCWSTR path, //!< [in] Path to the file, that will be added to the report.
102 LPCWSTR reportFileName /* = NULL */ //!< [in] Filename that will be used in report for this file. If parameter is \b NULL, original name from path will be used.
103 ) throw()
105 if (!m_AddFileToReport)
106 return false;
107 m_AddFileToReport(path, reportFileName);
108 return true;
111 //! Remove from report the file that was registered earlier to be sent within report.
112 //! \return If the function succeeds, the return value is \b true.
113 bool RemoveFileFromReport(
114 LPCWSTR path //!< [in] Path to the file, that will be removed from the report.
115 ) throw()
117 if (!m_RemoveFileFromReport)
118 return false;
119 m_RemoveFileFromReport(path);
120 return true;
123 //! Fills version field (V) of ApplicationInfo with product version
124 //! found in the executable file of the current process.
125 //! \return If the function succeeds, the return value is \b true.
126 bool GetVersionFromApp(
127 ApplicationInfo* appInfo //!< [out] Pointer to ApplicationInfo structure. Its version field (V) will be set to product version.
128 ) throw()
130 if (!m_GetVersionFromApp)
131 return false;
132 return m_GetVersionFromApp(appInfo) != FALSE;
135 //! Fill version field (V) of ApplicationInfo with product version found in the file specified.
136 //! \return If the function succeeds, the return value is \b true.
137 bool GetVersionFromFile(
138 LPCWSTR path, //!< [in] Path to the file product version will be extracted from.
139 ApplicationInfo* appInfo //!< [out] Pointer to ApplicationInfo structure. Its version field (V) will be set to product version.
140 ) throw()
142 if (!m_GetVersionFromFile)
143 return false;
144 return m_GetVersionFromFile(path, appInfo) != FALSE;
147 //! If you do not own the process your code running in (for example you write a plugin to some
148 //! external application) you need to properly initialize CrashHandler using \b ownProcess option.
149 //! Also you need to explicitly catch all exceptions in all entry points to your code and in all
150 //! threads you create. To do so use this construction:
151 //! \code
152 //! bool SomeEntryPoint(PARAM p)
153 //! {
154 //! __try
155 //! {
156 //! return YouCode(p);
157 //! }
158 //! __except (CrashHandler::SendReport(GetExceptionInformation()))
159 //! {
160 //! ::ExitProcess(0); // It is better to stop the process here or else corrupted data may incomprehensibly crash it later.
161 //! return false;
162 //! }
163 //! }
164 //! \endcode
165 LONG SendReport(
166 EXCEPTION_POINTERS* exceptionPointers //!< [in] Pointer to EXCEPTION_POINTERS structure. You should get it using GetExceptionInformation()
167 //!< function inside __except keyword.
170 if (!m_SendReport)
171 return EXCEPTION_CONTINUE_SEARCH;
172 // There is no crash handler but asserts should continue anyway
173 if (exceptionPointers->ExceptionRecord->ExceptionCode == ExceptionAssertionViolated)
174 return EXCEPTION_CONTINUE_EXECUTION;
175 return m_SendReport(exceptionPointers);
178 //! To send a report about violated assertion you can throw exception with this exception code
179 //! using: \code RaiseException(CrashHandler::ExceptionAssertionViolated, 0, 0, NULL); \endcode
180 //! Execution will continue after report will be sent (EXCEPTION_CONTINUE_EXECUTION would be used).
181 //! \note If you called CrashHandler constructor and crshhdnl.dll was missing you still may using this exception.
182 //! It will be catched, ignored and execution will continue. \ref SendReport function also works safely
183 //! when crshhdnl.dll was missing.
184 static const DWORD ExceptionAssertionViolated = ((DWORD)0xCCE17000);
186 //! Sends assertion violation report from this point and continue execution.
187 //! \sa ExceptionAssertionViolated
188 //! \note Functions prefixed with "CrashServer_" will be ignored in stack parsing.
189 void CrashServer_SendAssertionViolated() const
191 if (!m_InitCrashHandler)
192 return;
193 ::RaiseException(CrashHandler::ExceptionAssertionViolated, 0, 0, NULL);
196 private:
197 bool LoadDll() throw()
199 bool result = false;
201 // hCrshhndlDll should not be unloaded, crash may appear even after return from main().
202 // So hCrshhndlDll is not saved after construction.
203 BOOL bIsWow = FALSE;
204 IsWow64Process(GetCurrentProcess(), &bIsWow);
205 HMODULE hCrshhndlDll = nullptr;
206 if (bIsWow == FALSE)
207 hCrshhndlDll = ::LoadLibraryW(L"crshhndl.dll");
208 if (hCrshhndlDll != NULL)
210 m_InitCrashHandler = (pfnInitCrashHandler) GetProcAddress(hCrshhndlDll, "InitCrashHandler");
211 m_SendReport = (pfnSendReport) GetProcAddress(hCrshhndlDll, "SendReport");
212 m_IsReadyToExit = (pfnIsReadyToExit) GetProcAddress(hCrshhndlDll, "IsReadyToExit");
213 m_AddUserInfoToReport = (pfnAddUserInfoToReport) GetProcAddress(hCrshhndlDll, "AddUserInfoToReport");
214 m_AddFileToReport = (pfnAddFileToReport) GetProcAddress(hCrshhndlDll, "AddFileToReport");
215 m_RemoveFileFromReport = (pfnRemoveFileFromReport) GetProcAddress(hCrshhndlDll, "RemoveFileFromReport");
216 m_GetVersionFromApp = (pfnGetVersionFromApp) GetProcAddress(hCrshhndlDll, "GetVersionFromApp");
217 m_GetVersionFromFile = (pfnGetVersionFromFile) GetProcAddress(hCrshhndlDll, "GetVersionFromFile");
219 result = m_InitCrashHandler
220 && m_SendReport
221 && m_IsReadyToExit
222 && m_AddUserInfoToReport
223 && m_AddFileToReport
224 && m_RemoveFileFromReport
225 && m_GetVersionFromApp
226 && m_GetVersionFromFile;
229 return result;
232 static LONG CALLBACK SkipAsserts(EXCEPTION_POINTERS* pExceptionInfo)
234 if (pExceptionInfo->ExceptionRecord->ExceptionCode == ExceptionAssertionViolated)
235 return EXCEPTION_CONTINUE_EXECUTION;
236 return EXCEPTION_CONTINUE_SEARCH;
239 typedef BOOL (*pfnInitCrashHandler)(ApplicationInfo* applicationInfo, HandlerSettings* handlerSettings, BOOL ownProcess);
240 typedef LONG (*pfnSendReport)(EXCEPTION_POINTERS* exceptionPointers);
241 typedef BOOL (*pfnIsReadyToExit)();
242 typedef void (*pfnAddUserInfoToReport)(LPCWSTR key, LPCWSTR value);
243 typedef void (*pfnAddFileToReport)(LPCWSTR path, LPCWSTR reportFileName /* = NULL */);
244 typedef void (*pfnRemoveFileFromReport)(LPCWSTR path);
245 typedef BOOL (*pfnGetVersionFromApp)(ApplicationInfo* appInfo);
246 typedef BOOL (*pfnGetVersionFromFile)(LPCWSTR path, ApplicationInfo* appInfo);
248 pfnInitCrashHandler m_InitCrashHandler;
249 pfnSendReport m_SendReport;
250 pfnIsReadyToExit m_IsReadyToExit;
251 pfnAddUserInfoToReport m_AddUserInfoToReport;
252 pfnAddFileToReport m_AddFileToReport;
253 pfnRemoveFileFromReport m_RemoveFileFromReport;
254 pfnGetVersionFromApp m_GetVersionFromApp;
255 pfnGetVersionFromFile m_GetVersionFromFile;
259 class CCrashReportTGit
261 public:
263 //! Installs exception handlers to the caller process
264 CCrashReportTGit(LPCTSTR appname, USHORT versionMajor, USHORT versionMinor, USHORT versionMicro, USHORT versionBuild, const char * buildDate, bool bOwnProcess = true)
265 : m_nInstallStatus(0)
267 char s_month[6] = {0};
268 int month, day, year;
269 struct tm t = {0};
270 static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
271 sscanf_s(buildDate, "%s %d %d", s_month, (unsigned int)_countof(s_month) - 1, &day, &year);
272 month = (int)((strstr(month_names, s_month)-month_names))/3;
274 t.tm_mon = month;
275 t.tm_mday = day;
276 t.tm_year = year - 1900;
277 t.tm_isdst = -1;
278 __time64_t compiletime = _mktime64(&t);
280 __time64_t now;
281 _time64(&now);
283 #if PREVIEW
284 if ((now - compiletime) < (60 * 60 * 24 * 7 * 5))
285 #else
286 if ((now - compiletime) < (60 * 60 * 24 * 7 * 10))
287 #endif
289 ApplicationInfo appInfo;
290 memset(&appInfo, 0, sizeof(appInfo));
291 appInfo.ApplicationInfoSize = sizeof(ApplicationInfo);
292 appInfo.ApplicationGUID = "7fbde3fc-94e9-408b-b5c8-62bd4e203570";
293 appInfo.Prefix = "tgit";
294 appInfo.AppName = appname;
295 appInfo.Company = L"TortoiseGit";
297 appInfo.Hotfix = 0;
298 appInfo.V[0] = versionMajor;
299 appInfo.V[1] = versionMinor;
300 appInfo.V[2] = versionMicro;
301 appInfo.V[3] = versionBuild;
303 HandlerSettings handlerSettings;
304 memset(&handlerSettings, 0, sizeof(handlerSettings));
305 handlerSettings.HandlerSettingsSize = sizeof(handlerSettings);
306 handlerSettings.LeaveDumpFilesInTempFolder = FALSE;
307 handlerSettings.UseWER = FALSE;
308 handlerSettings.OpenProblemInBrowser = TRUE;
309 handlerSettings.SubmitterID = 0;
311 CCrashReport::Instance().InitCrashHandler(&appInfo, &handlerSettings, bOwnProcess);
316 //! Deinstalls exception handlers from the caller process
317 ~CCrashReportTGit()
319 CCrashReport::Instance().Uninstall();
322 //! Install status
323 int m_nInstallStatus;