TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
WheatyExceptionReport.h
Go to the documentation of this file.
1 #ifndef _WHEATYEXCEPTIONREPORT_
2 #define _WHEATYEXCEPTIONREPORT_
3 
4 #if PLATFORM == PLATFORM_WINDOWS && !defined(__MINGW32__)
5 
6 #include <winnt.h>
7 #include <winternl.h>
8 #include <dbghelp.h>
9 #include <set>
10 #include <stdlib.h>
11 #include <stack>
12 #include <mutex>
13 #define countof _countof
14 
15 #define WER_MAX_ARRAY_ELEMENTS_COUNT 10
16 #define WER_MAX_NESTING_LEVEL 5
17 #define WER_LARGE_BUFFER_SIZE 1024 * 128
18 
19 enum BasicType // Stolen from CVCONST.H in the DIA 2.0 SDK
20 {
21  btNoType = 0,
22  btVoid = 1,
23  btChar = 2,
24  btWChar = 3,
25  btInt = 6,
26  btUInt = 7,
27  btFloat = 8,
28  btBCD = 9,
29  btBool = 10,
30  btLong = 13,
31  btULong = 14,
32  btCurrency = 25,
33  btDate = 26,
34  btVariant = 27,
35  btComplex = 28,
36  btBit = 29,
37  btBSTR = 30,
38  btHresult = 31,
39 
40  // Custom types
42 };
43 
44 enum DataKind // Stolen from CVCONST.H in the DIA 2.0 SDK
45 {
56 };
57 
58 const char* const rgBaseType[] =
59 {
60  "<user defined>", // btNoType = 0,
61  "void", // btVoid = 1,
62  "char",//char* // btChar = 2,
63  "wchar_t*", // btWChar = 3,
64  "signed char",
65  "unsigned char",
66  "int", // btInt = 6,
67  "unsigned int", // btUInt = 7,
68  "float", // btFloat = 8,
69  "<BCD>", // btBCD = 9,
70  "bool", // btBool = 10,
71  "short",
72  "unsigned short",
73  "long", // btLong = 13,
74  "unsigned long", // btULong = 14,
75  "int8",
76  "int16",
77  "int32",
78  "int64",
79  "int128",
80  "uint8",
81  "uint16",
82  "uint32",
83  "uint64",
84  "uint128",
85  "<currency>", // btCurrency = 25,
86  "<date>", // btDate = 26,
87  "VARIANT", // btVariant = 27,
88  "<complex>", // btComplex = 28,
89  "<bit>", // btBit = 29,
90  "BSTR", // btBSTR = 30,
91  "HRESULT" // btHresult = 31
92 };
93 
94 struct SymbolPair
95 {
96  SymbolPair(DWORD type, DWORD_PTR offset)
97  {
98  _type = type;
99  _offset = offset;
100  }
101 
102  bool operator<(const SymbolPair& other) const
103  {
104  return _offset < other._offset ||
105  (_offset == other._offset && _type < other._type);
106  }
107 
110 };
111 typedef std::set<SymbolPair> SymbolPairs;
112 
114 {
116 
117  std::string ToString()
118  {
119  Logged = true;
120  std::string formatted = Prefix + Type + Suffix;
121  if (!Name.empty())
122  {
123  if (!formatted.empty())
124  formatted += " ";
125  formatted += Name;
126  }
127  if (!Value.empty())
128  formatted += " = " + Value;
129  return formatted;
130  }
131 
132  bool empty() const
133  {
134  return Value.empty() && !HasChildren;
135  }
136 
137  std::string Prefix;
138  std::string Type;
139  std::string Suffix;
140  std::string Name;
141  std::string Value;
142  bool Logged;
144 };
145 
147 {
148  public:
149 
152 
153  // entry point where control comes on an unhandled exception
155  PEXCEPTION_POINTERS pExceptionInfo);
156 
157  static void printTracesForAllThreads(bool);
158  private:
159  // where report info is extracted and generated
160  static void GenerateExceptionReport(PEXCEPTION_POINTERS pExceptionInfo);
161  static void PrintSystemInfo();
162  static BOOL _GetWindowsVersion(TCHAR* szVersion, DWORD cntMax);
163  static BOOL _GetProcessorName(TCHAR* sProcessorName, DWORD maxcount);
164 
165  // Helper functions
166  static LPTSTR GetExceptionString(DWORD dwCode);
167  static BOOL GetLogicalAddress(PVOID addr, PTSTR szModule, DWORD len,
168  DWORD& section, DWORD_PTR& offset);
169 
170  static void WriteStackDetails(PCONTEXT pContext, bool bWriteVariables, HANDLE pThreadHandle);
171 
172  static BOOL CALLBACK EnumerateSymbolsCallback(PSYMBOL_INFO, ULONG, PVOID);
173 
174  static bool FormatSymbolValue(PSYMBOL_INFO, STACKFRAME64 *, char * pszBuffer, unsigned cbBuffer);
175 
176  static char * DumpTypeIndex(char *, DWORD64, DWORD, unsigned, DWORD_PTR, bool &, const char*, char*, bool, bool);
177 
178  static void FormatOutputValue(char * pszCurrBuffer, BasicType basicType, DWORD64 length, PVOID pAddress, size_t bufferSize, size_t countOverride = 0);
179 
180  static BasicType GetBasicType(DWORD typeIndex, DWORD64 modBase);
182 
183  static int __cdecl _tprintf(const TCHAR * format, ...);
184  static int __cdecl stackprintf(const TCHAR * format, va_list argptr);
185  static int __cdecl heapprintf(const TCHAR * format, va_list argptr);
186 
187  static bool StoreSymbol(DWORD type , DWORD_PTR offset);
188  static void ClearSymbols();
189 
190  // Variables used by the class
193  static LPTOP_LEVEL_EXCEPTION_FILTER m_previousFilter;
198  static std::stack<SymbolDetail> symbolDetails;
200  static bool alreadyCrashed;
201  static std::mutex alreadyCrashedLock;
202  typedef NTSTATUS(NTAPI* pRtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation);
204 
205  static char* PushSymbolDetail(char* pszCurrBuffer);
206  static char* PopSymbolDetail(char* pszCurrBuffer);
207  static char* PrintSymbolDetail(char* pszCurrBuffer);
208 
209 };
210 
211 extern WheatyExceptionReport g_WheatyExceptionReport; // global instance of class
212 #endif // _WIN32
213 #endif // _WHEATYEXCEPTIONREPORT_
214 
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
bool HasChildren
Definition: WheatyExceptionReport.h:143
bool Logged
Definition: WheatyExceptionReport.h:142
static HANDLE m_hProcess
Definition: WheatyExceptionReport.h:196
static LPTSTR GetExceptionString(DWORD dwCode)
Definition: WheatyExceptionReport.cpp:590
const char *const rgBaseType[]
Definition: WheatyExceptionReport.h:58
static BasicType GetBasicType(DWORD typeIndex, DWORD64 modBase)
Definition: WheatyExceptionReport.cpp:1335
static void PrintSystemInfo()
Definition: WheatyExceptionReport.cpp:399
Definition: WheatyExceptionReport.h:32
static pRtlGetVersion RtlGetVersion
Definition: WheatyExceptionReport.h:203
Definition: WheatyExceptionReport.h:34
DWORD_PTR _offset
Definition: WheatyExceptionReport.h:109
std::string Type
Definition: WheatyExceptionReport.h:138
static BOOL _GetProcessorName(TCHAR *sProcessorName, DWORD maxcount)
Definition: WheatyExceptionReport.cpp:186
void * HANDLE
Definition: CascPort.h:146
static char * PrintSymbolDetail(char *pszCurrBuffer)
Definition: WheatyExceptionReport.cpp:1445
static std::stack< SymbolDetail > symbolDetails
Definition: WheatyExceptionReport.h:198
const size_t bufferSize
Definition: RASession.h:31
DWORD _type
Definition: WheatyExceptionReport.h:108
std::string Prefix
Definition: WheatyExceptionReport.h:137
static BOOL CALLBACK EnumerateSymbolsCallback(PSYMBOL_INFO, ULONG, PVOID)
Definition: WheatyExceptionReport.cpp:826
static LONG WINAPI WheatyUnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
Definition: WheatyExceptionReport.cpp:104
static BOOL GetLogicalAddress(PVOID addr, PTSTR szModule, DWORD len, DWORD &section, DWORD_PTR &offset)
Definition: WheatyExceptionReport.cpp:639
SymbolDetail()
Definition: WheatyExceptionReport.h:115
std::string Suffix
Definition: WheatyExceptionReport.h:139
static void GenerateExceptionReport(PEXCEPTION_POINTERS pExceptionInfo)
Definition: WheatyExceptionReport.cpp:473
Definition: WheatyExceptionReport.h:27
Definition: WheatyExceptionReport.h:26
Definition: WheatyExceptionReport.h:146
#define MAX_PATH
Definition: CascPort.h:160
#define false
Definition: CascPort.h:18
char TCHAR
Definition: CascPort.h:148
std::set< SymbolPair > SymbolPairs
Definition: WheatyExceptionReport.h:111
Definition: WheatyExceptionReport.h:37
int LONG
Definition: CascPort.h:138
static TCHAR m_szLogFileName[MAX_PATH]
Definition: WheatyExceptionReport.h:191
static bool stackOverflowException
Definition: WheatyExceptionReport.h:199
SymbolPair(DWORD type, DWORD_PTR offset)
Definition: WheatyExceptionReport.h:96
WheatyExceptionReport()
Definition: WheatyExceptionReport.cpp:74
Definition: WheatyExceptionReport.h:52
#define WINAPI
Definition: CascPort.h:163
static void FormatOutputValue(char *pszCurrBuffer, BasicType basicType, DWORD64 length, PVOID pAddress, size_t bufferSize, size_t countOverride=0)
Definition: WheatyExceptionReport.cpp:1256
Definition: WheatyExceptionReport.h:46
Definition: WheatyExceptionReport.h:48
Definition: WheatyExceptionReport.h:55
Definition: WheatyExceptionReport.h:47
Definition: WheatyExceptionReport.h:53
BasicType
Definition: WheatyExceptionReport.h:19
static BOOL _GetWindowsVersion(TCHAR *szVersion, DWORD cntMax)
Definition: WheatyExceptionReport.cpp:225
Definition: WheatyExceptionReport.h:54
static LPTOP_LEVEL_EXCEPTION_FILTER m_previousFilter
Definition: WheatyExceptionReport.h:193
std::string Value
Definition: WheatyExceptionReport.h:141
Definition: WheatyExceptionReport.h:51
static char * PopSymbolDetail(char *pszCurrBuffer)
Definition: WheatyExceptionReport.cpp:1438
Definition: WheatyExceptionReport.h:24
Definition: WheatyExceptionReport.h:113
unsigned long DWORD_PTR
Definition: CascPort.h:140
Definition: WheatyExceptionReport.h:41
Definition: WheatyExceptionReport.h:49
Definition: WheatyExceptionReport.h:35
static HANDLE m_hReportFile
Definition: WheatyExceptionReport.h:194
static void WriteStackDetails(PCONTEXT pContext, bool bWriteVariables, HANDLE pThreadHandle)
Definition: WheatyExceptionReport.cpp:704
Definition: WheatyExceptionReport.h:25
static void ClearSymbols()
Definition: WheatyExceptionReport.cpp:1423
bool empty() const
Definition: WheatyExceptionReport.h:132
static bool alreadyCrashed
Definition: WheatyExceptionReport.h:200
Definition: WheatyExceptionReport.h:23
bool operator<(const SymbolPair &other) const
Definition: WheatyExceptionReport.h:102
NTSTATUS(NTAPI * pRtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation)
Definition: WheatyExceptionReport.h:202
static int __cdecl heapprintf(const TCHAR *format, va_list argptr)
Definition: WheatyExceptionReport.cpp:1406
unsigned int DWORD
Definition: CascPort.h:139
float length(float v)
Definition: vectorMath.h:208
Definition: WheatyExceptionReport.h:22
static void printTracesForAllThreads(bool)
Definition: WheatyExceptionReport.cpp:423
WheatyExceptionReport g_WheatyExceptionReport
Definition: WheatyExceptionReport.cpp:70
static bool StoreSymbol(DWORD type, DWORD_PTR offset)
Definition: WheatyExceptionReport.cpp:1418
DataKind
Definition: WheatyExceptionReport.h:44
std::string Name
Definition: WheatyExceptionReport.h:140
static SymbolPairs symbols
Definition: WheatyExceptionReport.h:197
Definition: WheatyExceptionReport.h:36
std::string ToString()
Definition: WheatyExceptionReport.h:117
static bool FormatSymbolValue(PSYMBOL_INFO, STACKFRAME64 *, char *pszBuffer, unsigned cbBuffer)
Definition: WheatyExceptionReport.cpp:857
Definition: WheatyExceptionReport.h:31
static int __cdecl _tprintf(const TCHAR *format,...)
Definition: WheatyExceptionReport.cpp:1375
static TCHAR m_szDumpFileName[MAX_PATH]
Definition: WheatyExceptionReport.h:192
Definition: WheatyExceptionReport.h:21
static std::mutex alreadyCrashedLock
Definition: WheatyExceptionReport.h:201
Definition: WheatyExceptionReport.h:30
static char * DumpTypeIndex(char *, DWORD64, DWORD, unsigned, DWORD_PTR, bool &, const char *, char *, bool, bool)
Definition: WheatyExceptionReport.cpp:933
static int __cdecl stackprintf(const TCHAR *format, va_list argptr)
Definition: WheatyExceptionReport.cpp:1394
Definition: WheatyExceptionReport.h:28
~WheatyExceptionReport()
Definition: WheatyExceptionReport.cpp:94
Definition: WheatyExceptionReport.h:38
Type
Type of JSON value.
Definition: rapidjson.h:642
static HANDLE m_hDumpFile
Definition: WheatyExceptionReport.h:195
Definition: WheatyExceptionReport.h:29
Definition: WheatyExceptionReport.h:94
Definition: WheatyExceptionReport.h:50
static DWORD_PTR DereferenceUnsafePointer(DWORD_PTR address)
Definition: WheatyExceptionReport.cpp:1359
static char * PushSymbolDetail(char *pszCurrBuffer)
Definition: WheatyExceptionReport.cpp:1430
Definition: WheatyExceptionReport.h:33