BugSplat Native C++ SDK  3.6.0.13
BugSplat API reference for native Windows applications
BugSplat.h
1 
3 //
4 // BugSplat.h
5 //
6 // This header file contains the code used to add BugSplat crash reporting to
7 // native Windows applications.
8 //
9 // For more information, see the documentation at https://www.bugsplat.com/docs/platforms/cplusplus
10 //
11 // Copyright 2003-2019, BugSplat
12 // All rights reserved.
13 //
15 
20 
21 
22 #ifndef BUGSPLAT_H
23 #define BUGSPLAT_H
24 
25 #ifdef BUGSPLAT_EXPORTS
26  #define MDS_EXPORT __declspec(dllexport)
27 #else
28  #define MDS_EXPORT __declspec(dllimport)
29 #endif
30 
31 #include <eh.h>
32 #include <new.h>
33 #include <signal.h>
34 #include <Windows.h>
35 
36 
40 typedef bool (__cdecl *MiniDmpSenderCallback)(UINT nCode, LPVOID lVal1, LPVOID lVal2);
41 
42 // forward declare internal class
43 class MiniDumper;
44 
46 class MDS_EXPORT MiniDmpSender
47 {
48 public:
54  MiniDmpSender(__wchar_t const *szDatabase,
55  __wchar_t const *szApp,
56  __wchar_t const *szVersion,
57  __wchar_t const *szAppIdentifier = NULL,
58  DWORD dwFlags = 0x04 /* MDSF_PREVENTHIJACKING */
59  );
60  virtual ~MiniDmpSender();
61 public:
64  bool enableExceptionFilter(bool enable = true);
65 
67  bool isExceptionFilterEnabled() const;
68 
69  // the following typedef is identical to MINIDUMP_TYPE; copied here for convenience
70  typedef enum _BS_MINIDUMP_TYPE {
71  MiniDumpNormal = 0x00000000,
72  MiniDumpWithDataSegs = 0x00000001,
73  MiniDumpWithFullMemory = 0x00000002,
74  MiniDumpWithHandleData = 0x00000004,
75  MiniDumpFilterMemory = 0x00000008,
76  MiniDumpScanMemory = 0x00000010,
77  MiniDumpWithUnloadedModules = 0x00000020,
78  MiniDumpWithIndirectlyReferencedMemory = 0x00000040,
79  MiniDumpFilterModulePaths = 0x00000080,
80  MiniDumpWithProcessThreadData = 0x00000100,
81  MiniDumpWithPrivateReadWriteMemory = 0x00000200,
82  MiniDumpWithoutOptionalData = 0x00000400,
83  MiniDumpWithFullMemoryInfo = 0x00000800,
84  MiniDumpWithThreadInfo = 0x00001000,
85  MiniDumpWithCodeSegs = 0x00002000,
86  MiniDumpWithoutAuxiliaryState = 0x00004000,
87  MiniDumpWithFullAuxiliaryState = 0x00008000,
88  MiniDumpWithPrivateWriteCopyMemory = 0x00010000,
89  MiniDumpIgnoreInaccessibleMemory = 0x00020000,
90  MiniDumpWithTokenInformation = 0x00040000,
91  MiniDumpWithModuleHeaders = 0x00080000,
92  MiniDumpFilterTriage = 0x00100000,
93  MiniDumpWithAvxXStateContext = 0x00200000,
94  MiniDumpValidTypeFlags = 0x003fffff,
95  } BS_MINIDUMP_TYPE;
96 
98  MiniDmpSender::BS_MINIDUMP_TYPE getMiniDumpType() const;
99 
102  void setMiniDumpType(MiniDmpSender::BS_MINIDUMP_TYPE eType);
103 
106  bool disableNetworkAccess(bool enable = true);
108  bool isNetworkAccessDisabled() const;
109 
111  bool setFlags( DWORD dwFlags );
113  DWORD getFlags() const;
114 
116  void resetVersionString(const __wchar_t * wszVersion);
117 
119  void resetAppIdentifier(const __wchar_t * wszDescription);
120 
122  void setNotes(const __wchar_t* wszNotes);
123 
125  void sendAdditionalFile(const __wchar_t * wszPath);
126 
128  bool removeAdditionalFile(const __wchar_t * wszPath);
129 
131  void setLogFilePath(const __wchar_t * wszPath);
132 
134  void setUserZipPath(const __wchar_t * wszPath);
135 
137  void setResourceDllPath(const __wchar_t * wszPath);
138 
140  void setDefaultUserName(const __wchar_t * wszName);
141 
143  void setDefaultUserEmail(const __wchar_t * wszEmail);
144 
146  void setDefaultUserDescription(const __wchar_t * wszDescription);
147 
150  void createReport(EXCEPTION_POINTERS * pExcepInfo);
151 
153  void createAsanReport(const char* asanMessage);
154 
158  void createReport(const __wchar_t * wszXmlReport);
159 
161  inline void createReportAndExit() { setFlags(getFlags() | 0x02/*MDSF_FORCEEXIT*/); throw NULL; }
162 
164  LONG unhandledExceptionHandler( PEXCEPTION_POINTERS pExceptionInfo );
165 
167  void setCallback(MiniDmpSenderCallback fnCallback);
168 
170  void getMinidumpPath(__wchar_t * buffer, size_t len);
171 
173  int setGuardByteBufferSize(int nbytes);
174 
176  LPVOID imp();
177 
178 private:
179  MiniDumper * m_pMiniDumper;
180 };
181 
182 
183 // ***************************
184 // Definitions for the dwFlags parameter of MiniDmpSender constructor and the getFlags/setFlags methods.
185 // The non-interactive flag is for applications that run unattended or without a user interface.
186 
188 #define MDSF_NONINTERACTIVE 0x0001
189 
192 #define MDSF_FORCEEXIT 0x0002
193 
198 #define MDSF_PREVENTHIJACKING 0x0004
199 
205 #define MDSF_DETECTHANGS 0x0008
206 
210 #define MDSF_USEGUARDMEMORY 0x0010
211 
216 #define MDSF_CUSTOMEXCEPTIONFILTER 0x0020
217 
223 #define MDSF_SUSPENDALLTHREADS 0x0800
224 
228 #define MDSF_LOGCONSOLE 0x1000
229 #define MDSF_LOGFILE 0x2000
230 #define MDSF_LOG_VERBOSE 0x4000
231 
233 
237 #define MDSCB_EXCEPTIONCODE 0x0050
238 
239 //
240 // Helper functions used to set CRT state.
241 //
242 inline void terminator() { int*z = 0; *z = 13; }
243 inline void signal_handler(int) { terminator(); }
244 inline void __cdecl invalid_parameter_handler(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
245 {
246  terminator();
247 }
248 inline int memory_depleted(size_t)
249 {
250  terminator();
251  return 0;
252 }
253 
254 // This call should be made once from your app to enable collection of certain CRT exceptions
255 inline void SetGlobalCRTExceptionBehavior()
256 {
257  // There is a single set_terminate handler for all dynamically linked DLLs or EXEs;
258  // even if you call set_terminate your handler may be replaced by another,
259  // or you may be replacing a handler set by another DLL or EXE.
260  // See https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/set-terminate-crt?view=vs-2019
261  set_terminate(&terminator);
262 
263  // Because there is only one _purecall_handler for each process, when you call _set_purecall_handler
264  // it immediately impacts all threads. The last caller on any thread sets the handler.
265  // See https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-purecall-handler-set-purecall-handler?view=vs-2019
266  _set_purecall_handler(&terminator);
267 
268  // Only one function can be specified as the global invalid argument handler at a time.
269  // See https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/set-invalid-parameter-handler-set-thread-local-invalid-parameter-handler?view=vs-2019
270  _set_invalid_parameter_handler(&invalid_parameter_handler);
271 
272  // There is a single _set_new_handler handler for all dynamically linked DLLs or executables;
273  // even if you call _set_new_handler your handler might be replaced by another or that you are
274  // replacing a handler set by another DLL or executable.
275  // See https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/set-new-handler?view=vs-2019
276  _set_new_handler(&memory_depleted);
277  _set_new_mode(1);
278 }
279 
280 
281 // This call should be made in each thread of your application to enable collection of certain CRT exceptions
282 inline void SetPerThreadCRTExceptionBehavior()
283 {
284  // Signal handling, required for each thread, at least for SIGABRT
285  signal(SIGABRT, signal_handler);
286  _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
287 
288 
289  // In a multithreaded environment, unexpected functions are maintained separately for each thread.
290  // Each new thread needs to install its own unexpected function. Thus, each thread is in charge of
291  // its own unexpected handling.
292  // See https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/set-unexpected-crt?view=vs-2019
293  set_unexpected(&terminator);
294 }
295 
296 #endif //~BUGSPLAT_H
void createReportAndExit()
Use to send a report and exit.
Definition: BugSplat.h:161
The MiniDmpSender class is used to add BugSplat crash reporting to your app.
Definition: BugSplat.h:46