foobar2000 SDK  2015-08-03
fb2kdebug.h
Go to the documentation of this file.
1 extern "C"
2 {
3  LPCSTR SHARED_EXPORT uGetCallStackPath();
4  LONG SHARED_EXPORT uExceptFilterProc(LPEXCEPTION_POINTERS param);
5  PFC_NORETURN void SHARED_EXPORT uBugCheck();
6 
7 #ifdef _DEBUG
8  void SHARED_EXPORT fb2kDebugSelfTest();
9 #endif
10 }
11 
13 {
15 public:
16  explicit SHARED_EXPORT uCallStackTracker(const char * name);
17  SHARED_EXPORT ~uCallStackTracker();
18 };
19 
20 
21 
22 #if 1
23 #define TRACK_CALL(X) uCallStackTracker TRACKER__##X(#X)
24 #define TRACK_CALL_TEXT(X) uCallStackTracker TRACKER__BLAH(X)
25 #define TRACK_CODE(description,code) {uCallStackTracker __call_tracker(description); code;}
26 #else
27 #define TRACK_CALL(X)
28 #define TRACK_CALL_TEXT(X)
29 #define TRACK_CODE(description,code) {code;}
30 #endif
31 
32 static int uExceptFilterProc_inline(LPEXCEPTION_POINTERS param) {
33  uDumpCrashInfo(param);
34  TerminateProcess(GetCurrentProcess(), 0);
35  return 0;// never reached
36 }
37 
38 
39 #define FB2K_DYNAMIC_ASSERT( X ) { if (!(X)) uBugCheck(); }
40 
41 #define __except_instacrash __except(uExceptFilterProc(GetExceptionInformation()))
42 #define fb2k_instacrash_scope(X) __try { X; } __except_instacrash {}
43 
44 PFC_NORETURN static void fb2kCriticalError(DWORD code, DWORD argCount = 0, const ULONG_PTR * args = NULL) {
45  fb2k_instacrash_scope( RaiseException(code,EXCEPTION_NONCONTINUABLE,argCount,args); );
46 }
47 PFC_NORETURN static void fb2kDeadlock() {
48  fb2kCriticalError(0x63d81b66);
49 }
50 
51 static void fb2kWaitForCompletion(HANDLE hEvent) {
52  switch(WaitForSingleObject(hEvent, INFINITE)) {
53  case WAIT_OBJECT_0:
54  return;
55  default:
56  uBugCheck();
57  }
58 }
59 
60 static void fb2kWaitForThreadCompletion(HANDLE hWaitFor, DWORD threadID) {
61  switch(WaitForSingleObject(hWaitFor, INFINITE)) {
62  case WAIT_OBJECT_0:
63  return;
64  default:
65  uBugCheck();
66  }
67 }
68 
69 static void fb2kWaitForThreadCompletion2(HANDLE hWaitFor, HANDLE hThread, DWORD threadID) {
70  switch(WaitForSingleObject(hWaitFor, INFINITE)) {
71  case WAIT_OBJECT_0:
72  return;
73  default:
74  uBugCheck();
75  }
76 }
77 
78 
79 static void __cdecl _OverrideCrtAbort_handler(int signal) {
80  const ULONG_PTR args[] = {signal};
81  RaiseException(0x6F8E1DC8 /* random GUID */, EXCEPTION_NONCONTINUABLE, _countof(args), args);
82 }
83 
84 static void __cdecl _PureCallHandler() {
85  RaiseException(0xf6538887 /* random GUID */, EXCEPTION_NONCONTINUABLE, 0, 0);
86 }
87 
88 static void _InvalidParameter(
89  const wchar_t * expression,
90  const wchar_t * function,
91  const wchar_t * file,
92  unsigned int line,
93  uintptr_t pReserved
94 ) {
95  RaiseException(0xd142b808 /* random GUID */, EXCEPTION_NONCONTINUABLE, 0, 0);
96 }
97 
98 static void OverrideCrtAbort() {
99  const int signals[] = {SIGINT, SIGTERM, SIGBREAK, SIGABRT};
100  for(size_t i=0; i<_countof(signals); i++) signal(signals[i], _OverrideCrtAbort_handler);
101  _set_abort_behavior(0, ~0);
102 
103  _set_purecall_handler(_PureCallHandler);
104  _set_invalid_parameter_handler(_InvalidParameter);
105 }
LONG SHARED_EXPORT uExceptFilterProc(LPEXCEPTION_POINTERS param)
void SHARED_EXPORT uDumpCrashInfo(LPEXCEPTION_POINTERS param)
SHARED_EXPORT uCallStackTracker(const char *name)
LPCSTR SHARED_EXPORT uGetCallStackPath()
static void __cdecl _OverrideCrtAbort_handler(int signal)
Definition: fb2kdebug.h:79
static void fb2kWaitForThreadCompletion2(HANDLE hWaitFor, HANDLE hThread, DWORD threadID)
Definition: fb2kdebug.h:69
static void fb2kWaitForCompletion(HANDLE hEvent)
Definition: fb2kdebug.h:51
static PFC_NORETURN void fb2kCriticalError(DWORD code, DWORD argCount=0, const ULONG_PTR *args=NULL)
Definition: fb2kdebug.h:44
SHARED_EXPORT ~uCallStackTracker()
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
static PFC_NORETURN void fb2kDeadlock()
Definition: fb2kdebug.h:47
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
Definition: pp-winapi.h:16
static void _InvalidParameter(const wchar_t *expression, const wchar_t *function, const wchar_t *file, unsigned int line, uintptr_t pReserved)
Definition: fb2kdebug.h:88
size_t t_size
Definition: int_types.h:48
static int uExceptFilterProc_inline(LPEXCEPTION_POINTERS param)
Definition: fb2kdebug.h:32
void SHARED_EXPORT fb2kDebugSelfTest()
static void fb2kWaitForThreadCompletion(HANDLE hWaitFor, DWORD threadID)
Definition: fb2kdebug.h:60
static void OverrideCrtAbort()
Definition: fb2kdebug.h:98
static void __cdecl _PureCallHandler()
Definition: fb2kdebug.h:84
PFC_NORETURN void SHARED_EXPORT uBugCheck()