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.
21 #include "../../ext/CrashServer/CrashHandler/CrashHandler/CrashHandler.h"
26 // dummy define, needed only when we use crashrpt instead of this.
27 #define CR_AF_MAKE_FILE_COPY 0
31 * helper class for the CrashServerSDK
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)
54 static CCrashReport
& Instance()
56 static CCrashReport 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.
78 if (!m_InitCrashHandler
)
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.
91 if (!m_AddUserInfoToReport
)
93 m_AddUserInfoToReport(key
, value
);
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.
105 if (!m_AddFileToReport
)
107 m_AddFileToReport(path
, reportFileName
);
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.
117 if (!m_RemoveFileFromReport
)
119 m_RemoveFileFromReport(path
);
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.
130 if (!m_GetVersionFromApp
)
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.
142 if (!m_GetVersionFromFile
)
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:
152 //! bool SomeEntryPoint(PARAM p)
156 //! return YouCode(p);
158 //! __except (CrashHandler::SendReport(GetExceptionInformation()))
160 //! ::ExitProcess(0); // It is better to stop the process here or else corrupted data may incomprehensibly crash it later.
166 EXCEPTION_POINTERS
* exceptionPointers
//!< [in] Pointer to EXCEPTION_POINTERS structure. You should get it using GetExceptionInformation()
167 //!< function inside __except keyword.
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
)
193 ::RaiseException(CrashHandler::ExceptionAssertionViolated
, 0, 0, NULL
);
197 bool LoadDll() throw()
201 // hCrshhndlDll should not be unloaded, crash may appear even after return from main().
202 // So hCrshhndlDll is not saved after construction.
204 IsWow64Process(GetCurrentProcess(), &bIsWow
);
205 HMODULE hCrshhndlDll
= nullptr;
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
222 && m_AddUserInfoToReport
224 && m_RemoveFileFromReport
225 && m_GetVersionFromApp
226 && m_GetVersionFromFile
;
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
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
;
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;
276 t
.tm_year
= year
- 1900;
278 __time64_t compiletime
= _mktime64(&t
);
284 if ((now
- compiletime
) < (60 * 60 * 24 * 7 * 5))
286 if ((now
- compiletime
) < (60 * 60 * 24 * 7 * 10))
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";
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
319 CCrashReport::Instance().Uninstall();
323 int m_nInstallStatus
;