Debian Codegraph @

3rdparty/xspublic/xscommon/stackwalker.h

raw symbols SCIP
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 /**********************************************************************
34 *
35 * StackWalker.h
36 *
37 *
38 * History:
39 * 2005-07-27 v1 - First public release on http://www.codeproject.com/
40 * (for additional changes see History in 'StackWalker.cpp'!
41 *
42 **********************************************************************/
43 #ifndef STACKWALKER_H
44 #define STACKWALKER_H
45
46 #ifndef __GNUC__
47
48 // #pragma once is supported starting with _MCS_VER 1000,
49 // so we need not to check the version (because we only support _MSC_VER >= 1100)!
50 //#pragma once
51
52 #include <windows.h>
53
54 // special defines for VC5/6 (if no actual PSDK is installed):
55 #if _MSC_VER < 1300
56 typedef unsigned __int64 DWORD64, *PDWORD64;
57 #if defined(_WIN64)
58 typedef unsigned __int64 SIZE_T, *PSIZE_T;
59 #else
60 typedef unsigned long SIZE_T, *PSIZE_T;
61 #endif
62 #endif // _MSC_VER < 1300
63
64 class StackWalkerInternal;
65 /*! \brief A class that can help with walking the stack for debugging purposes (Windows version) */
66 class StackWalker {
67 public:
68 enum StackWalkOptions
69 {
70 // No addition info will be retrived
71 // (only the address is available)
72 RetrieveNone = 0,
73
74 // Try to get the symbol-name
75 RetrieveSymbol = 1,
76
77 // Try to get the line for this symbol
78 RetrieveLine = 2,
79
80 // Try to retrieve the module-infos
81 RetrieveModuleInfo = 4,
82
83 // Also retrieve the version for the DLL/EXE
84 RetrieveFileVersion = 8,
85
86 // Contains all the above
87 RetrieveVerbose = 0xF,
88
89 // Generate a "good" symbol-search-path
90 SymBuildPath = 0x10,
91
92 // Also use the public Microsoft-Symbol-Server
93 SymUseSymSrv = 0x20,
94
95 // Contains all the abouve "Sym"-options
96 SymAll = 0x30,
97
98 // Contains all options (default)
99 OptionsAll = 0x3F
100 };
101
102 StackWalker(int options = (int) OptionsAll, // 'int' is by design, to combine the enum-flags
103 LPCSTR szSymPath = NULL,
104 DWORD dwProcessId = GetCurrentProcessId(),
105 HANDLE hProcess = GetCurrentProcess());
106 StackWalker(DWORD dwProcessId, HANDLE hProcess);
107 virtual ~StackWalker();
108
109 typedef BOOL (__stdcall *PReadProcessMemoryRoutine)(
110 HANDLE hProcess,
111 DWORD64 qwBaseAddress,
112 PVOID lpBuffer,
113 DWORD nSize,
114 LPDWORD lpNumberOfBytesRead,
115 LPVOID pUserData // optional data, which was passed in "ShowCallstack"
116 );
117
118 BOOL LoadModules();
119
120 BOOL ShowCallstack(HANDLE hThread = GetCurrentThread(),
121 const CONTEXT *context = NULL,
122 PReadProcessMemoryRoutine readMemoryFunction = NULL,
123 LPVOID pUserData = NULL // optional to identify some data in the 'readMemoryFunction'-callback
124 );
125
126 protected:
127 enum { STACKWALK_MAX_NAMELEN = 1024 }; // max name length for found symbols
128
129 /*! \brief Container for each Callstack-Entry */
130 struct CallstackEntry
131 {
132 DWORD64 offset; // if 0, we have no valid entry
133 CHAR name[STACKWALK_MAX_NAMELEN];
134 CHAR undName[STACKWALK_MAX_NAMELEN];
135 CHAR undFullName[STACKWALK_MAX_NAMELEN];
136 DWORD64 offsetFromSmybol;
137 DWORD offsetFromLine;
138 DWORD lineNumber;
139 CHAR lineFileName[STACKWALK_MAX_NAMELEN];
140 DWORD symType;
141 LPCSTR symTypeString;
142 CHAR moduleName[STACKWALK_MAX_NAMELEN];
143 DWORD64 baseOfImage;
144 CHAR loadedImageName[STACKWALK_MAX_NAMELEN];
145 };
146
147 enum CallstackEntryType {firstEntry, nextEntry, lastEntry};
148
149 virtual void OnSymInit(LPCSTR szSearchPath, DWORD symOptions, LPCSTR szUserName);
150 virtual void OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion);
151 virtual void OnCallstackEntry(CallstackEntryType eType, CallstackEntry &entry);
152 virtual void OnDbgHelpErr(LPCSTR szFuncName, DWORD gle, DWORD64 addr);
153 virtual void OnOutput(LPCSTR szText);
154
155 StackWalkerInternal *m_sw;
156 HANDLE m_hProcess;
157 DWORD m_dwProcessId;
158 BOOL m_modulesLoaded;
159 LPSTR m_szSymPath;
160
161 int m_options;
162
163 static BOOL __stdcall myReadProcMem(HANDLE hProcess, DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead);
164
165 friend StackWalkerInternal;
166 StackWalker(StackWalker const&) = delete;
167 StackWalker& operator = (StackWalker const&) = delete;
168 };
169
170 // The "ugly" assembler-implementation is needed for systems before XP
171 // If you have a new PSDK and you only compile for XP and later, then you can use
172 // the "RtlCaptureContext"
173 // Currently there is no define which determines the PSDK-Version...
174 // So we just use the compiler-version (and assumes that the PSDK is
175 // the one which was installed by the VS-IDE)
176
177 // INFO: If you want, you can use the RtlCaptureContext if you only target XP and later...
178 // But I currently use it in x64/IA64 environments...
179 //#if defined(_M_IX86) && (_WIN32_WINNT <= 0x0500) && (_MSC_VER < 1400)
180
181 #if defined(_M_IX86)
182 #ifdef CURRENT_THREAD_VIA_EXCEPTION
183 // The following is not a "perfect" implementation,
184 // because the callstack is only valid in the "__except" block...
185 #define GET_CURRENT_CONTEXT(c, contextFlags) \
186 do { \
187 memset(&c, 0, sizeof(CONTEXT)); \
188 EXCEPTION_POINTERS *pExp = NULL; \
189 __try { \
190 throw 0; \
191 } __except( ( (pExp = GetExceptionInformation()) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_EXECUTE_HANDLER)) {} \
192 if (pExp != NULL) \
193 memcpy(&c, pExp->ContextRecord, sizeof(CONTEXT)); \
194 c.ContextFlags = contextFlags; \
195 } while(0);
196 #else
197 // The following should be enough for walking the callstack...
198 #define GET_CURRENT_CONTEXT(c, contextFlags) \
199 do { \
200 memset(&c, 0, sizeof(CONTEXT)); \
201 c.ContextFlags = contextFlags; \
202 __asm call x \
203 __asm x: pop eax \
204 __asm mov c.Eip, eax \
205 __asm mov c.Ebp, ebp \
206 __asm mov c.Esp, esp \
207 } while(0);
208 #endif
209
210 #else
211
212 // The following is defined for x86 (XP and higher), x64 and IA64:
213 #define GET_CURRENT_CONTEXT(c, contextFlags) \
214 do { \
215 memset(&c, 0, sizeof(CONTEXT)); \
216 c.ContextFlags = contextFlags; \
217 RtlCaptureContext(&c); \
218 } while(0);
219 #endif
220
221 #endif
222
223 #endif