Developer Documentation

StackWalker.cc
1 /**********************************************************************
2 *
3 * StackWalker.cpp
4 * https://github.com/JochenKalmbach/StackWalker
5 *
6 * Old location: http://stackwalker.codeplex.com/
7 *
8 *
9 * History:
10 * 2005-07-27 v1 - First public release on http://www.codeproject.com/
11 * http://www.codeproject.com/threads/StackWalker.asp
12 * 2005-07-28 v2 - Changed the params of the constructor and ShowCallstack
13 * (to simplify the usage)
14 * 2005-08-01 v3 - Changed to use 'CONTEXT_FULL' instead of CONTEXT_ALL
15 * (should also be enough)
16 * - Changed to compile correctly with the PSDK of VC7.0
17 * (GetFileVersionInfoSizeA and GetFileVersionInfoA is wrongly defined:
18 * it uses LPSTR instead of LPCSTR as first paremeter)
19 * - Added declarations to support VC5/6 without using 'dbghelp.h'
20 * - Added a 'pUserData' member to the ShowCallstack function and the
21 * PReadProcessMemoryRoutine declaration (to pass some user-defined data,
22 * which can be used in the readMemoryFunction-callback)
23 * 2005-08-02 v4 - OnSymInit now also outputs the OS-Version by default
24 * - Added example for doing an exception-callstack-walking in main.cpp
25 * (thanks to owillebo: http://www.codeproject.com/script/profile/whos_who.asp?id=536268)
26 * 2005-08-05 v5 - Removed most Lint (http://www.gimpel.com/) errors... thanks to Okko Willeboordse!
27 * 2008-08-04 v6 - Fixed Bug: Missing LEAK-end-tag
28 * http://www.codeproject.com/KB/applications/leakfinder.aspx?msg=2502890#xx2502890xx
29 * Fixed Bug: Compiled with "WIN32_LEAN_AND_MEAN"
30 * http://www.codeproject.com/KB/applications/leakfinder.aspx?msg=1824718#xx1824718xx
31 * Fixed Bug: Compiling with "/Wall"
32 * http://www.codeproject.com/KB/threads/StackWalker.aspx?msg=2638243#xx2638243xx
33 * Fixed Bug: Now checking SymUseSymSrv
34 * http://www.codeproject.com/KB/threads/StackWalker.aspx?msg=1388979#xx1388979xx
35 * Fixed Bug: Support for recursive function calls
36 * http://www.codeproject.com/KB/threads/StackWalker.aspx?msg=1434538#xx1434538xx
37 * Fixed Bug: Missing FreeLibrary call in "GetModuleListTH32"
38 * http://www.codeproject.com/KB/threads/StackWalker.aspx?msg=1326923#xx1326923xx
39 * Fixed Bug: SymDia is number 7, not 9!
40 * 2008-09-11 v7 For some (undocumented) reason, dbhelp.h is needing a packing of 8!
41 * Thanks to Teajay which reported the bug...
42 * http://www.codeproject.com/KB/applications/leakfinder.aspx?msg=2718933#xx2718933xx
43 * 2008-11-27 v8 Debugging Tools for Windows are now stored in a different directory
44 * Thanks to Luiz Salamon which reported this "bug"...
45 * http://www.codeproject.com/KB/threads/StackWalker.aspx?msg=2822736#xx2822736xx
46 * 2009-04-10 v9 License slihtly corrected (<ORGANIZATION> replaced)
47 * 2009-11-01 v10 Moved to http://stackwalker.codeplex.com/
48 * 2009-11-02 v11 Now try to use IMAGEHLP_MODULE64_V3 if available
49 * 2010-04-15 v12 Added support for VS2010 RTM
50 * 2010-05-25 v13 Now using secure MyStrcCpy. Thanks to luke.simon:
51 * http://www.codeproject.com/KB/applications/leakfinder.aspx?msg=3477467#xx3477467xx
52 * 2013-01-07 v14 Runtime Check Error VS2010 Debug Builds fixed:
53 * http://stackwalker.codeplex.com/workitem/10511
54 *
55 *
56 * LICENSE (http://www.opensource.org/licenses/bsd-license.php)
57 *
58 * Copyright (c) 2005-2013, Jochen Kalmbach
59 * All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without modification,
62 * are permitted provided that the following conditions are met:
63 *
64 * Redistributions of source code must retain the above copyright notice,
65 * this list of conditions and the following disclaimer.
66 * Redistributions in binary form must reproduce the above copyright notice,
67 * this list of conditions and the following disclaimer in the documentation
68 * and/or other materials provided with the distribution.
69 * Neither the name of Jochen Kalmbach nor the names of its contributors may be
70 * used to endorse or promote products derived from this software without
71 * specific prior written permission.
72 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
73 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
74 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
77 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
78 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
79 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
80 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
81 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82 *
83 **********************************************************************/
84 #include <windows.h>
85 #include <tchar.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #pragma comment(lib, "version.lib") // for "VerQueryValue"
89 #pragma warning(disable:4826)
90
91 #include "StackWalker.hh"
92
93
94 // If VC7 and later, then use the shipped 'dbghelp.h'-file
95 #pragma pack(push,8)
96 #if _MSC_VER >= 1300
97 #include <dbghelp.h>
98 #else
99 // inline the important dbghelp.h-declarations...
100 typedef enum {
101 SymNone = 0,
102 SymCoff,
103 SymCv,
104 SymPdb,
105 SymExport,
106 SymDeferred,
107 SymSym,
108 SymDia,
109 SymVirtual,
110 NumSymTypes
111 } SYM_TYPE;
112 typedef struct _IMAGEHLP_LINE64 {
113 DWORD SizeOfStruct; // set to sizeof(IMAGEHLP_LINE64)
114 PVOID Key; // internal
115 DWORD LineNumber; // line number in file
116 PCHAR FileName; // full filename
117 DWORD64 Address; // first instruction of line
118 } IMAGEHLP_LINE64, *PIMAGEHLP_LINE64;
119 typedef struct _IMAGEHLP_MODULE64 {
120 DWORD SizeOfStruct; // set to sizeof(IMAGEHLP_MODULE64)
121 DWORD64 BaseOfImage; // base load address of module
122 DWORD ImageSize; // virtual size of the loaded module
123 DWORD TimeDateStamp; // date/time stamp from pe header
124 DWORD CheckSum; // checksum from the pe header
125 DWORD NumSyms; // number of symbols in the symbol table
126 SYM_TYPE SymType; // type of symbols loaded
127 CHAR ModuleName[32]; // module name
128 CHAR ImageName[256]; // image name
129 CHAR LoadedImageName[256]; // symbol file name
130 } IMAGEHLP_MODULE64, *PIMAGEHLP_MODULE64;
131 typedef struct _IMAGEHLP_SYMBOL64 {
132 DWORD SizeOfStruct; // set to sizeof(IMAGEHLP_SYMBOL64)
133 DWORD64 Address; // virtual address including dll base address
134 DWORD Size; // estimated size of symbol, can be zero
135 DWORD Flags; // info about the symbols, see the SYMF defines
136 DWORD MaxNameLength; // maximum size of symbol name in 'Name'
137 CHAR Name[1]; // symbol name (null terminated string)
138 } IMAGEHLP_SYMBOL64, *PIMAGEHLP_SYMBOL64;
139 typedef enum {
140 AddrMode1616,
141 AddrMode1632,
142 AddrModeReal,
143 AddrModeFlat
144 } ADDRESS_MODE;
145 typedef struct _tagADDRESS64 {
146 DWORD64 Offset;
147 WORD Segment;
148 ADDRESS_MODE Mode;
149 } ADDRESS64, *LPADDRESS64;
150 typedef struct _KDHELP64 {
151 DWORD64 Thread;
152 DWORD ThCallbackStack;
153 DWORD ThCallbackBStore;
154 DWORD NextCallback;
155 DWORD FramePointer;
156 DWORD64 KiCallUserMode;
157 DWORD64 KeUserCallbackDispatcher;
158 DWORD64 SystemRangeStart;
159 DWORD64 Reserved[8];
160 } KDHELP64, *PKDHELP64;
161 typedef struct _tagSTACKFRAME64 {
162 ADDRESS64 AddrPC; // program counter
163 ADDRESS64 AddrReturn; // return address
164 ADDRESS64 AddrFrame; // frame pointer
165 ADDRESS64 AddrStack; // stack pointer
166 ADDRESS64 AddrBStore; // backing store pointer
167 PVOID FuncTableEntry; // pointer to pdata/fpo or NULL
168 DWORD64 Params[4]; // possible arguments to the function
169 BOOL Far; // WOW far call
170 BOOL Virtual; // is this a virtual frame?
171 DWORD64 Reserved[3];
172 KDHELP64 KdHelp;
173 } STACKFRAME64, *LPSTACKFRAME64;
174 typedef
175 BOOL
176 (__stdcall *PREAD_PROCESS_MEMORY_ROUTINE64)(
177 HANDLE hProcess,
178 DWORD64 qwBaseAddress,
179 PVOID lpBuffer,
180 DWORD nSize,
181 LPDWORD lpNumberOfBytesRead
182 );
183 typedef
184 PVOID
185 (__stdcall *PFUNCTION_TABLE_ACCESS_ROUTINE64)(
186 HANDLE hProcess,
187 DWORD64 AddrBase
188 );
189 typedef
190 DWORD64
191 (__stdcall *PGET_MODULE_BASE_ROUTINE64)(
192 HANDLE hProcess,
193 DWORD64 Address
194 );
195 typedef
196 DWORD64
197 (__stdcall *PTRANSLATE_ADDRESS_ROUTINE64)(
198 HANDLE hProcess,
199 HANDLE hThread,
200 LPADDRESS64 lpaddr
201 );
202 #define SYMOPT_CASE_INSENSITIVE 0x00000001
203 #define SYMOPT_UNDNAME 0x00000002
204 #define SYMOPT_DEFERRED_LOADS 0x00000004
205 #define SYMOPT_NO_CPP 0x00000008
206 #define SYMOPT_LOAD_LINES 0x00000010
207 #define SYMOPT_OMAP_FIND_NEAREST 0x00000020
208 #define SYMOPT_LOAD_ANYTHING 0x00000040
209 #define SYMOPT_IGNORE_CVREC 0x00000080
210 #define SYMOPT_NO_UNQUALIFIED_LOADS 0x00000100
211 #define SYMOPT_FAIL_CRITICAL_ERRORS 0x00000200
212 #define SYMOPT_EXACT_SYMBOLS 0x00000400
213 #define SYMOPT_ALLOW_ABSOLUTE_SYMBOLS 0x00000800
214 #define SYMOPT_IGNORE_NT_SYMPATH 0x00001000
215 #define SYMOPT_INCLUDE_32BIT_MODULES 0x00002000
216 #define SYMOPT_PUBLICS_ONLY 0x00004000
217 #define SYMOPT_NO_PUBLICS 0x00008000
218 #define SYMOPT_AUTO_PUBLICS 0x00010000
219 #define SYMOPT_NO_IMAGE_SEARCH 0x00020000
220 #define SYMOPT_SECURE 0x00040000
221 #define SYMOPT_DEBUG 0x80000000
222 #define UNDNAME_COMPLETE (0x0000) // Enable full undecoration
223 #define UNDNAME_NAME_ONLY (0x1000) // Crack only the name for primary declaration;
224 #endif // _MSC_VER < 1300
225 #pragma pack(pop)
226
227 // Some missing defines (for VC5/6):
228 #ifndef INVALID_FILE_ATTRIBUTES
229 #define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
230 #endif
231
232
233 // secure-CRT_functions are only available starting with VC8
234 #if _MSC_VER < 1400
235 #define strcpy_s(dst, len, src) strcpy(dst, src)
236 #define strncpy_s(dst, len, src, maxLen) strncpy(dst, len, src)
237 #define strcat_s(dst, len, src) strcat(dst, src)
238 #define _snprintf_s _snprintf
239 #define _tcscat_s _tcscat
240 #endif
241
242 static void MyStrCpy(char* szDest, size_t nMaxDestSize, const char* szSrc)
243 {
244 if (nMaxDestSize <= 0) return;
245 strncpy_s(szDest, nMaxDestSize, szSrc, _TRUNCATE);
246 szDest[nMaxDestSize-1] = 0; // INFO: _TRUNCATE will ensure that it is nul-terminated; but with older compilers (<1400) it uses "strncpy" and this does not!)
247 } // MyStrCpy
248
249 // Normally it should be enough to use 'CONTEXT_FULL' (better would be 'CONTEXT_ALL')
250 #define USED_CONTEXT_FLAGS CONTEXT_FULL
251
252
253 class StackWalkerInternal
254 {
255 public:
256 StackWalkerInternal(StackWalker *parent, HANDLE hProcess)
257 {
258 m_parent = parent;
259 m_hDbhHelp = NULL;
260 pSC = NULL;
261 m_hProcess = hProcess;
262 m_szSymPath = NULL;
263 pSFTA = NULL;
264 pSGLFA = NULL;
265 pSGMB = NULL;
266 pSGMI = NULL;
267 pSGO = NULL;
268 pSGSFA = NULL;
269 pSI = NULL;
270 pSLM = NULL;
271 pSSO = NULL;
272 pSW = NULL;
273 pUDSN = NULL;
274 pSGSP = NULL;
275 }
276 ~StackWalkerInternal()
277 {
278 if (pSC != NULL)
279 pSC(m_hProcess); // SymCleanup
280 if (m_hDbhHelp != NULL)
281 FreeLibrary(m_hDbhHelp);
282 m_hDbhHelp = NULL;
283 m_parent = NULL;
284 if(m_szSymPath != NULL)
285 free(m_szSymPath);
286 m_szSymPath = NULL;
287 }
288 BOOL Init(LPCSTR szSymPath)
289 {
290 if (m_parent == NULL)
291 return FALSE;
292 // Dynamically load the Entry-Points for dbghelp.dll:
293 // First try to load the newsest one from
294 TCHAR szTemp[4096];
295 // But before wqe do this, we first check if the ".local" file exists
296 if (GetModuleFileName(NULL, szTemp, 4096) > 0)
297 {
298 _tcscat_s(szTemp, _T(".local"));
299 if (GetFileAttributes(szTemp) == INVALID_FILE_ATTRIBUTES)
300 {
301 // ".local" file does not exist, so we can try to load the dbghelp.dll from the "Debugging Tools for Windows"
302 // Ok, first try the new path according to the archtitecture:
303 #ifdef _M_IX86
304 if ( (m_hDbhHelp == NULL) && (GetEnvironmentVariable(_T("ProgramFiles"), szTemp, 4096) > 0) )
305 {
306 _tcscat_s(szTemp, _T("\\Debugging Tools for Windows (x86)\\dbghelp.dll"));
307 // now check if the file exists:
308 if (GetFileAttributes(szTemp) != INVALID_FILE_ATTRIBUTES)
309 {
310 m_hDbhHelp = LoadLibrary(szTemp);
311 }
312 }
313 #elif _M_X64
314 if ( (m_hDbhHelp == NULL) && (GetEnvironmentVariable(_T("ProgramFiles"), szTemp, 4096) > 0) )
315 {
316 _tcscat_s(szTemp, _T("\\Debugging Tools for Windows (x64)\\dbghelp.dll"));
317 // now check if the file exists:
318 if (GetFileAttributes(szTemp) != INVALID_FILE_ATTRIBUTES)
319 {
320 m_hDbhHelp = LoadLibrary(szTemp);
321 }
322 }
323 #elif _M_IA64
324 if ( (m_hDbhHelp == NULL) && (GetEnvironmentVariable(_T("ProgramFiles"), szTemp, 4096) > 0) )
325 {
326 _tcscat_s(szTemp, _T("\\Debugging Tools for Windows (ia64)\\dbghelp.dll"));
327 // now check if the file exists:
328 if (GetFileAttributes(szTemp) != INVALID_FILE_ATTRIBUTES)
329 {
330 m_hDbhHelp = LoadLibrary(szTemp);
331 }
332 }
333 #endif
334 // If still not found, try the old directories...
335 if ( (m_hDbhHelp == NULL) && (GetEnvironmentVariable(_T("ProgramFiles"), szTemp, 4096) > 0) )
336 {
337 _tcscat_s(szTemp, _T("\\Debugging Tools for Windows\\dbghelp.dll"));
338 // now check if the file exists:
339 if (GetFileAttributes(szTemp) != INVALID_FILE_ATTRIBUTES)
340 {
341 m_hDbhHelp = LoadLibrary(szTemp);
342 }
343 }
344 #if defined _M_X64 || defined _M_IA64
345 // Still not found? Then try to load the (old) 64-Bit version:
346 if ( (m_hDbhHelp == NULL) && (GetEnvironmentVariable(_T("ProgramFiles"), szTemp, 4096) > 0) )
347 {
348 _tcscat_s(szTemp, _T("\\Debugging Tools for Windows 64-Bit\\dbghelp.dll"));
349 if (GetFileAttributes(szTemp) != INVALID_FILE_ATTRIBUTES)
350 {
351 m_hDbhHelp = LoadLibrary(szTemp);
352 }
353 }
354 #endif
355 }
356 }
357 if (m_hDbhHelp == NULL) // if not already loaded, try to load a default-one
358 m_hDbhHelp = LoadLibrary( _T("dbghelp.dll") );
359 if (m_hDbhHelp == NULL)
360 return FALSE;
361 pSI = (tSI) GetProcAddress(m_hDbhHelp, "SymInitialize" );
362 pSC = (tSC) GetProcAddress(m_hDbhHelp, "SymCleanup" );
363
364 pSW = (tSW) GetProcAddress(m_hDbhHelp, "StackWalk64" );
365 pSGO = (tSGO) GetProcAddress(m_hDbhHelp, "SymGetOptions" );
366 pSSO = (tSSO) GetProcAddress(m_hDbhHelp, "SymSetOptions" );
367
368 pSFTA = (tSFTA) GetProcAddress(m_hDbhHelp, "SymFunctionTableAccess64" );
369 pSGLFA = (tSGLFA) GetProcAddress(m_hDbhHelp, "SymGetLineFromAddr64" );
370 pSGMB = (tSGMB) GetProcAddress(m_hDbhHelp, "SymGetModuleBase64" );
371 pSGMI = (tSGMI) GetProcAddress(m_hDbhHelp, "SymGetModuleInfo64" );
372 pSGSFA = (tSGSFA) GetProcAddress(m_hDbhHelp, "SymGetSymFromAddr64" );
373 pUDSN = (tUDSN) GetProcAddress(m_hDbhHelp, "UnDecorateSymbolName" );
374 pSLM = (tSLM) GetProcAddress(m_hDbhHelp, "SymLoadModule64" );
375 pSGSP =(tSGSP) GetProcAddress(m_hDbhHelp, "SymGetSearchPath" );
376
377 if ( pSC == NULL || pSFTA == NULL || pSGMB == NULL || pSGMI == NULL ||
378 pSGO == NULL || pSGSFA == NULL || pSI == NULL || pSSO == NULL ||
379 pSW == NULL || pUDSN == NULL || pSLM == NULL )
380 {
381 FreeLibrary(m_hDbhHelp);
382 m_hDbhHelp = NULL;
383 pSC = NULL;
384 return FALSE;
385 }
386
387 // SymInitialize
388 if (szSymPath != NULL)
389 m_szSymPath = _strdup(szSymPath);
390 if (this->pSI(m_hProcess, m_szSymPath, FALSE) == FALSE)
391 this->m_parent->OnDbgHelpErr("SymInitialize", GetLastError(), 0);
392
393 DWORD symOptions = this->pSGO(); // SymGetOptions
394 symOptions |= SYMOPT_LOAD_LINES;
395 symOptions |= SYMOPT_FAIL_CRITICAL_ERRORS;
396 //symOptions |= SYMOPT_NO_PROMPTS;
397 // SymSetOptions
398 symOptions = this->pSSO(symOptions);
399
400 char buf[StackWalker::STACKWALK_MAX_NAMELEN] = {0};
401 if (this->pSGSP != NULL)
402 {
403 if (this->pSGSP(m_hProcess, buf, StackWalker::STACKWALK_MAX_NAMELEN) == FALSE)
404 this->m_parent->OnDbgHelpErr("SymGetSearchPath", GetLastError(), 0);
405 }
406 char szUserName[1024] = {0};
407 DWORD dwSize = 1024;
408 GetUserNameA(szUserName, &dwSize);
409 this->m_parent->OnSymInit(buf, symOptions, szUserName);
410
411 return TRUE;
412 }
413
414 StackWalker *m_parent;
415
416 HMODULE m_hDbhHelp;
417 HANDLE m_hProcess;
418 LPSTR m_szSymPath;
419
420 #pragma pack(push,8)
421 typedef struct IMAGEHLP_MODULE64_V3 {
422 DWORD SizeOfStruct; // set to sizeof(IMAGEHLP_MODULE64)
423 DWORD64 BaseOfImage; // base load address of module
424 DWORD ImageSize; // virtual size of the loaded module
425 DWORD TimeDateStamp; // date/time stamp from pe header
426 DWORD CheckSum; // checksum from the pe header
427 DWORD NumSyms; // number of symbols in the symbol table
428 SYM_TYPE SymType; // type of symbols loaded
429 CHAR ModuleName[32]; // module name
430 CHAR ImageName[256]; // image name
431 CHAR LoadedImageName[256]; // symbol file name
432 // new elements: 07-Jun-2002
433 CHAR LoadedPdbName[256]; // pdb file name
434 DWORD CVSig; // Signature of the CV record in the debug directories
435 CHAR CVData[MAX_PATH * 3]; // Contents of the CV record
436 DWORD PdbSig; // Signature of PDB
437 GUID PdbSig70; // Signature of PDB (VC 7 and up)
438 DWORD PdbAge; // DBI age of pdb
439 BOOL PdbUnmatched; // loaded an unmatched pdb
440 BOOL DbgUnmatched; // loaded an unmatched dbg
441 BOOL LineNumbers; // we have line number information
442 BOOL GlobalSymbols; // we have internal symbol information
443 BOOL TypeInfo; // we have type information
444 // new elements: 17-Dec-2003
445 BOOL SourceIndexed; // pdb supports source server
446 BOOL Publics; // contains public symbols
447 };
448
449 typedef struct IMAGEHLP_MODULE64_V2 {
450 DWORD SizeOfStruct; // set to sizeof(IMAGEHLP_MODULE64)
451 DWORD64 BaseOfImage; // base load address of module
452 DWORD ImageSize; // virtual size of the loaded module
453 DWORD TimeDateStamp; // date/time stamp from pe header
454 DWORD CheckSum; // checksum from the pe header
455 DWORD NumSyms; // number of symbols in the symbol table
456 SYM_TYPE SymType; // type of symbols loaded
457 CHAR ModuleName[32]; // module name
458 CHAR ImageName[256]; // image name
459 CHAR LoadedImageName[256]; // symbol file name
460 };
461 #pragma pack(pop)
462
463
464 // SymCleanup()
465 typedef BOOL (__stdcall *tSC)( IN HANDLE hProcess );
466 tSC pSC;
467
468 // SymFunctionTableAccess64()
469 typedef PVOID (__stdcall *tSFTA)( HANDLE hProcess, DWORD64 AddrBase );
470 tSFTA pSFTA;
471
472 // SymGetLineFromAddr64()
473 typedef BOOL (__stdcall *tSGLFA)( IN HANDLE hProcess, IN DWORD64 dwAddr,
474 OUT PDWORD pdwDisplacement, OUT PIMAGEHLP_LINE64 Line );
475 tSGLFA pSGLFA;
476
477 // SymGetModuleBase64()
478 typedef DWORD64 (__stdcall *tSGMB)( IN HANDLE hProcess, IN DWORD64 dwAddr );
479 tSGMB pSGMB;
480
481 // SymGetModuleInfo64()
482 typedef BOOL (__stdcall *tSGMI)( IN HANDLE hProcess, IN DWORD64 dwAddr, OUT IMAGEHLP_MODULE64_V3 *ModuleInfo );
483 tSGMI pSGMI;
484
485 // SymGetOptions()
486 typedef DWORD (__stdcall *tSGO)( VOID );
487 tSGO pSGO;
488
489 // SymGetSymFromAddr64()
490 typedef BOOL (__stdcall *tSGSFA)( IN HANDLE hProcess, IN DWORD64 dwAddr,
491 OUT PDWORD64 pdwDisplacement, OUT PIMAGEHLP_SYMBOL64 Symbol );
492 tSGSFA pSGSFA;
493
494 // SymInitialize()
495 typedef BOOL (__stdcall *tSI)( IN HANDLE hProcess, IN PSTR UserSearchPath, IN BOOL fInvadeProcess );
496 tSI pSI;
497
498 // SymLoadModule64()
499 typedef DWORD64 (__stdcall *tSLM)( IN HANDLE hProcess, IN HANDLE hFile,
500 IN PSTR ImageName, IN PSTR ModuleName, IN DWORD64 BaseOfDll, IN DWORD SizeOfDll );
501 tSLM pSLM;
502
503 // SymSetOptions()
504 typedef DWORD (__stdcall *tSSO)( IN DWORD SymOptions );
505 tSSO pSSO;
506
507 // StackWalk64()
508 typedef BOOL (__stdcall *tSW)(
509 DWORD MachineType,
510 HANDLE hProcess,
511 HANDLE hThread,
512 LPSTACKFRAME64 StackFrame,
513 PVOID ContextRecord,
514 PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine,
515 PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,
516 PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine,
517 PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress );
518 tSW pSW;
519
520 // UnDecorateSymbolName()
521 typedef DWORD (__stdcall WINAPI *tUDSN)( PCSTR DecoratedName, PSTR UnDecoratedName,
522 DWORD UndecoratedLength, DWORD Flags );
523 tUDSN pUDSN;
524
525 typedef BOOL (__stdcall WINAPI *tSGSP)(HANDLE hProcess, PSTR SearchPath, DWORD SearchPathLength);
526 tSGSP pSGSP;
527
528
529 private:
530 // **************************************** ToolHelp32 ************************
531 #define MAX_MODULE_NAME32 255
532 #define TH32CS_SNAPMODULE 0x00000008
533 #pragma pack( push, 8 )
534 typedef struct tagMODULEENTRY32
535 {
536 DWORD dwSize;
537 DWORD th32ModuleID; // This module
538 DWORD th32ProcessID; // owning process
539 DWORD GlblcntUsage; // Global usage count on the module
540 DWORD ProccntUsage; // Module usage count in th32ProcessID's context
541 BYTE * modBaseAddr; // Base address of module in th32ProcessID's context
542 DWORD modBaseSize; // Size in bytes of module starting at modBaseAddr
543 HMODULE hModule; // The hModule of this module in th32ProcessID's context
544 char szModule[MAX_MODULE_NAME32 + 1];
545 char szExePath[MAX_PATH];
546 } MODULEENTRY32;
547 typedef MODULEENTRY32 * PMODULEENTRY32;
548 typedef MODULEENTRY32 * LPMODULEENTRY32;
549 #pragma pack( pop )
550
551 BOOL GetModuleListTH32(HANDLE hProcess, DWORD pid)
552 {
553 // CreateToolhelp32Snapshot()
554 typedef HANDLE (__stdcall *tCT32S)(DWORD dwFlags, DWORD th32ProcessID);
555 // Module32First()
556 typedef BOOL (__stdcall *tM32F)(HANDLE hSnapshot, LPMODULEENTRY32 lpme);
557 // Module32Next()
558 typedef BOOL (__stdcall *tM32N)(HANDLE hSnapshot, LPMODULEENTRY32 lpme);
559
560 // try both dlls...
561 const TCHAR *dllname[] = { _T("kernel32.dll"), _T("tlhelp32.dll") };
562 HINSTANCE hToolhelp = NULL;
563 tCT32S pCT32S = NULL;
564 tM32F pM32F = NULL;
565 tM32N pM32N = NULL;
566
567 HANDLE hSnap;
568 MODULEENTRY32 me;
569 me.dwSize = sizeof(me);
570 BOOL keepGoing;
571 size_t i;
572
573 for (i = 0; i<(sizeof(dllname) / sizeof(dllname[0])); i++ )
574 {
575 hToolhelp = LoadLibrary( dllname[i] );
576 if (hToolhelp == NULL)
577 continue;
578 pCT32S = (tCT32S) GetProcAddress(hToolhelp, "CreateToolhelp32Snapshot");
579 pM32F = (tM32F) GetProcAddress(hToolhelp, "Module32First");
580 pM32N = (tM32N) GetProcAddress(hToolhelp, "Module32Next");
581 if ( (pCT32S != NULL) && (pM32F != NULL) && (pM32N != NULL) )
582 break; // found the functions!
583 FreeLibrary(hToolhelp);
584 hToolhelp = NULL;
585 }
586
587 if (hToolhelp == NULL)
588 return FALSE;
589
590 hSnap = pCT32S( TH32CS_SNAPMODULE, pid );
591 if (hSnap == (HANDLE) -1)
592 {
593 FreeLibrary(hToolhelp);
594 return FALSE;
595 }
596
597 keepGoing = !!pM32F( hSnap, &me );
598 int cnt = 0;
599 while (keepGoing)
600 {
601 this->LoadModule(hProcess, me.szExePath, me.szModule, (DWORD64) me.modBaseAddr, me.modBaseSize);
602 cnt++;
603 keepGoing = !!pM32N( hSnap, &me );
604 }
605 CloseHandle(hSnap);
606 FreeLibrary(hToolhelp);
607 if (cnt <= 0)
608 return FALSE;
609 return TRUE;
610 } // GetModuleListTH32
611
612 // **************************************** PSAPI ************************
613 typedef struct _MODULEINFO {
614 LPVOID lpBaseOfDll;
615 DWORD SizeOfImage;
616 LPVOID EntryPoint;
617 } MODULEINFO, *LPMODULEINFO;
618
619 BOOL GetModuleListPSAPI(HANDLE hProcess)
620 {
621 // EnumProcessModules()
622 typedef BOOL (__stdcall *tEPM)(HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded );
623 // GetModuleFileNameEx()
624 typedef DWORD (__stdcall *tGMFNE)(HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize );
625 // GetModuleBaseName()
626 typedef DWORD (__stdcall *tGMBN)(HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize );
627 // GetModuleInformation()
628 typedef BOOL (__stdcall *tGMI)(HANDLE hProcess, HMODULE hModule, LPMODULEINFO pmi, DWORD nSize );
629
630 HINSTANCE hPsapi;
631 tEPM pEPM;
632 tGMFNE pGMFNE;
633 tGMBN pGMBN;
634 tGMI pGMI;
635
636 DWORD i;
637 //ModuleEntry e;
638 DWORD cbNeeded;
639 MODULEINFO mi;
640 HMODULE *hMods = 0;
641 char *tt = NULL;
642 char *tt2 = NULL;
643 const SIZE_T TTBUFLEN = 8096;
644 int cnt = 0;
645
646 hPsapi = LoadLibrary( _T("psapi.dll") );
647 if (hPsapi == NULL)
648 return FALSE;
649
650 pEPM = (tEPM) GetProcAddress( hPsapi, "EnumProcessModules" );
651 pGMFNE = (tGMFNE) GetProcAddress( hPsapi, "GetModuleFileNameExA" );
652 pGMBN = (tGMFNE) GetProcAddress( hPsapi, "GetModuleBaseNameA" );
653 pGMI = (tGMI) GetProcAddress( hPsapi, "GetModuleInformation" );
654 if ( (pEPM == NULL) || (pGMFNE == NULL) || (pGMBN == NULL) || (pGMI == NULL) )
655 {
656 // we couldn't find all functions
657 FreeLibrary(hPsapi);
658 return FALSE;
659 }
660
661 hMods = (HMODULE*) malloc(sizeof(HMODULE) * (TTBUFLEN / sizeof(HMODULE)));
662 tt = (char*) malloc(sizeof(char) * TTBUFLEN);
663 tt2 = (char*) malloc(sizeof(char) * TTBUFLEN);
664 if ( (hMods == NULL) || (tt == NULL) || (tt2 == NULL) )
665 goto cleanup;
666
667 if ( ! pEPM( hProcess, hMods, TTBUFLEN, &cbNeeded ) )
668 {
669 //_ftprintf(fLogFile, _T("%lu: EPM failed, GetLastError = %lu\n"), g_dwShowCount, gle );
670 goto cleanup;
671 }
672
673 if ( cbNeeded > TTBUFLEN )
674 {
675 //_ftprintf(fLogFile, _T("%lu: More than %lu module handles. Huh?\n"), g_dwShowCount, lenof( hMods ) );
676 goto cleanup;
677 }
678
679 for ( i = 0; i < cbNeeded / sizeof(hMods[0]); i++ )
680 {
681 // base address, size
682 pGMI(hProcess, hMods[i], &mi, sizeof(mi));
683 // image file name
684 tt[0] = 0;
685 pGMFNE(hProcess, hMods[i], tt, TTBUFLEN );
686 // module name
687 tt2[0] = 0;
688 pGMBN(hProcess, hMods[i], tt2, TTBUFLEN );
689
690 DWORD dwRes = this->LoadModule(hProcess, tt, tt2, (DWORD64) mi.lpBaseOfDll, mi.SizeOfImage);
691 if (dwRes != ERROR_SUCCESS)
692 this->m_parent->OnDbgHelpErr("LoadModule", dwRes, 0);
693 cnt++;
694 }
695
696 cleanup:
697 if (hPsapi != NULL) FreeLibrary(hPsapi);
698 if (tt2 != NULL) free(tt2);
699 if (tt != NULL) free(tt);
700 if (hMods != NULL) free(hMods);
701
702 return cnt != 0;
703 } // GetModuleListPSAPI
704
705 DWORD LoadModule(HANDLE hProcess, LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size)
706 {
707 CHAR *szImg = _strdup(img);
708 CHAR *szMod = _strdup(mod);
709 DWORD result = ERROR_SUCCESS;
710 if ( (szImg == NULL) || (szMod == NULL) )
711 result = ERROR_NOT_ENOUGH_MEMORY;
712 else
713 {
714 if (pSLM(hProcess, 0, szImg, szMod, baseAddr, size) == 0)
715 result = GetLastError();
716 }
717 ULONGLONG fileVersion = 0;
718 if ( (m_parent != NULL) && (szImg != NULL) )
719 {
720 // try to retrive the file-version:
721 if ( (this->m_parent->m_options & StackWalker::RetrieveFileVersion) != 0)
722 {
723 VS_FIXEDFILEINFO *fInfo = NULL;
724 DWORD dwHandle;
725 DWORD dwSize = GetFileVersionInfoSizeA(szImg, &dwHandle);
726 if (dwSize > 0)
727 {
728 LPVOID vData = malloc(dwSize);
729 if (vData != NULL)
730 {
731 if (GetFileVersionInfoA(szImg, dwHandle, dwSize, vData) != 0)
732 {
733 UINT len;
734 TCHAR szSubBlock[] = _T("\\");
735 if (VerQueryValue(vData, szSubBlock, (LPVOID*) &fInfo, &len) == 0)
736 fInfo = NULL;
737 else
738 {
739 fileVersion = ((ULONGLONG)fInfo->dwFileVersionLS) + ((ULONGLONG)fInfo->dwFileVersionMS << 32);
740 }
741 }
742 free(vData);
743 }
744 }
745 }
746
747 // Retrive some additional-infos about the module
748 IMAGEHLP_MODULE64_V3 Module;
749 const char *szSymType = "-unknown-";
750 if (this->GetModuleInfo(hProcess, baseAddr, &Module) != FALSE)
751 {
752 switch(Module.SymType)
753 {
754 case SymNone:
755 szSymType = "-nosymbols-";
756 break;
757 case SymCoff: // 1
758 szSymType = "COFF";
759 break;
760 case SymCv: // 2
761 szSymType = "CV";
762 break;
763 case SymPdb: // 3
764 szSymType = "PDB";
765 break;
766 case SymExport: // 4
767 szSymType = "-exported-";
768 break;
769 case SymDeferred: // 5
770 szSymType = "-deferred-";
771 break;
772 case SymSym: // 6
773 szSymType = "SYM";
774 break;
775 case 7: // SymDia:
776 szSymType = "DIA";
777 break;
778 case 8: //SymVirtual:
779 szSymType = "Virtual";
780 break;
781 }
782 }
783 LPCSTR pdbName = Module.LoadedImageName;
784 if (Module.LoadedPdbName[0] != 0)
785 pdbName = Module.LoadedPdbName;
786 this->m_parent->OnLoadModule(img, mod, baseAddr, size, result, szSymType, pdbName, fileVersion);
787 }
788 if (szImg != NULL) free(szImg);
789 if (szMod != NULL) free(szMod);
790 return result;
791 }
792 public:
793 BOOL LoadModules(HANDLE hProcess, DWORD dwProcessId)
794 {
795 // first try toolhelp32
796 if (GetModuleListTH32(hProcess, dwProcessId))
797 return true;
798 // then try psapi
799 return GetModuleListPSAPI(hProcess);
800 }
801
802
803 BOOL GetModuleInfo(HANDLE hProcess, DWORD64 baseAddr, IMAGEHLP_MODULE64_V3 *pModuleInfo)
804 {
805 memset(pModuleInfo, 0, sizeof(IMAGEHLP_MODULE64_V3));
806 if(this->pSGMI == NULL)
807 {
808 SetLastError(ERROR_DLL_INIT_FAILED);
809 return FALSE;
810 }
811 // First try to use the larger ModuleInfo-Structure
812 pModuleInfo->SizeOfStruct = sizeof(IMAGEHLP_MODULE64_V3);
813 void *pData = malloc(4096); // reserve enough memory, so the bug in v6.3.5.1 does not lead to memory-overwrites...
814 if (pData == NULL)
815 {
816 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
817 return FALSE;
818 }
819 memcpy(pData, pModuleInfo, sizeof(IMAGEHLP_MODULE64_V3));
820 static bool s_useV3Version = true;
821 if (s_useV3Version)
822 {
823 if (this->pSGMI(hProcess, baseAddr, (IMAGEHLP_MODULE64_V3*) pData) != FALSE)
824 {
825 // only copy as much memory as is reserved...
826 memcpy(pModuleInfo, pData, sizeof(IMAGEHLP_MODULE64_V3));
827 pModuleInfo->SizeOfStruct = sizeof(IMAGEHLP_MODULE64_V3);
828 free(pData);
829 return TRUE;
830 }
831 s_useV3Version = false; // to prevent unneccessarry calls with the larger struct...
832 }
833
834 // could not retrive the bigger structure, try with the smaller one (as defined in VC7.1)...
835 pModuleInfo->SizeOfStruct = sizeof(IMAGEHLP_MODULE64_V2);
836 memcpy(pData, pModuleInfo, sizeof(IMAGEHLP_MODULE64_V2));
837 if (this->pSGMI(hProcess, baseAddr, (IMAGEHLP_MODULE64_V3*) pData) != FALSE)
838 {
839 // only copy as much memory as is reserved...
840 memcpy(pModuleInfo, pData, sizeof(IMAGEHLP_MODULE64_V2));
841 pModuleInfo->SizeOfStruct = sizeof(IMAGEHLP_MODULE64_V2);
842 free(pData);
843 return TRUE;
844 }
845 free(pData);
846 SetLastError(ERROR_DLL_INIT_FAILED);
847 return FALSE;
848 }
849 };
850
851 // #############################################################
852 StackWalker::StackWalker(DWORD dwProcessId, HANDLE hProcess)
853 {
854 this->m_options = OptionsAll;
855 this->m_modulesLoaded = FALSE;
856 this->m_hProcess = hProcess;
857 this->m_sw = new StackWalkerInternal(this, this->m_hProcess);
858 this->m_dwProcessId = dwProcessId;
859 this->m_szSymPath = NULL;
860 this->m_MaxRecursionCount = 1000;
861 }
862 StackWalker::StackWalker(int options, LPCSTR szSymPath, DWORD dwProcessId, HANDLE hProcess)
863 {
864 this->m_options = options;
865 this->m_modulesLoaded = FALSE;
866 this->m_hProcess = hProcess;
867 this->m_sw = new StackWalkerInternal(this, this->m_hProcess);
868 this->m_dwProcessId = dwProcessId;
869 if (szSymPath != NULL)
870 {
871 this->m_szSymPath = _strdup(szSymPath);
872 this->m_options |= SymBuildPath;
873 }
874 else
875 this->m_szSymPath = NULL;
876 this->m_MaxRecursionCount = 1000;
877 }
878
879 StackWalker::~StackWalker()
880 {
881 if (m_szSymPath != NULL)
882 free(m_szSymPath);
883 m_szSymPath = NULL;
884 if (this->m_sw != NULL)
885 delete this->m_sw;
886 this->m_sw = NULL;
887 }
888
889 BOOL StackWalker::LoadModules()
890 {
891 if (this->m_sw == NULL)
892 {
893 SetLastError(ERROR_DLL_INIT_FAILED);
894 return FALSE;
895 }
896 if (m_modulesLoaded != FALSE)
897 return TRUE;
898
899 // Build the sym-path:
900 char *szSymPath = NULL;
901 if ( (this->m_options & SymBuildPath) != 0)
902 {
903 const size_t nSymPathLen = 4096;
904 szSymPath = (char*) malloc(nSymPathLen);
905 if (szSymPath == NULL)
906 {
907 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
908 return FALSE;
909 }
910 szSymPath[0] = 0;
911 // Now first add the (optional) provided sympath:
912 if (this->m_szSymPath != NULL)
913 {
914 strcat_s(szSymPath, nSymPathLen, this->m_szSymPath);
915 strcat_s(szSymPath, nSymPathLen, ";");
916 }
917
918 strcat_s(szSymPath, nSymPathLen, ".;");
919
920 const size_t nTempLen = 1024;
921 char szTemp[nTempLen];
922 // Now add the current directory:
923 if (GetCurrentDirectoryA(nTempLen, szTemp) > 0)
924 {
925 szTemp[nTempLen-1] = 0;
926 strcat_s(szSymPath, nSymPathLen, szTemp);
927 strcat_s(szSymPath, nSymPathLen, ";");
928 }
929
930 // Now add the path for the main-module:
931 if (GetModuleFileNameA(NULL, szTemp, nTempLen) > 0)
932 {
933 szTemp[nTempLen-1] = 0;
934 for (char *p = (szTemp+strlen(szTemp)-1); p >= szTemp; --p)
935 {
936 // locate the rightmost path separator
937 if ( (*p == '\\') || (*p == '/') || (*p == ':') )
938 {
939 *p = 0;
940 break;
941 }
942 } // for (search for path separator...)
943 if (strlen(szTemp) > 0)
944 {
945 strcat_s(szSymPath, nSymPathLen, szTemp);
946 strcat_s(szSymPath, nSymPathLen, ";");
947 }
948 }
949 if (GetEnvironmentVariableA("_NT_SYMBOL_PATH", szTemp, nTempLen) > 0)
950 {
951 szTemp[nTempLen-1] = 0;
952 strcat_s(szSymPath, nSymPathLen, szTemp);
953 strcat_s(szSymPath, nSymPathLen, ";");
954 }
955 if (GetEnvironmentVariableA("_NT_ALTERNATE_SYMBOL_PATH", szTemp, nTempLen) > 0)
956 {
957 szTemp[nTempLen-1] = 0;
958 strcat_s(szSymPath, nSymPathLen, szTemp);
959 strcat_s(szSymPath, nSymPathLen, ";");
960 }
961 if (GetEnvironmentVariableA("SYSTEMROOT", szTemp, nTempLen) > 0)
962 {
963 szTemp[nTempLen-1] = 0;
964 strcat_s(szSymPath, nSymPathLen, szTemp);
965 strcat_s(szSymPath, nSymPathLen, ";");
966 // also add the "system32"-directory:
967 strcat_s(szTemp, nTempLen, "\\system32");
968 strcat_s(szSymPath, nSymPathLen, szTemp);
969 strcat_s(szSymPath, nSymPathLen, ";");
970 }
971
972 if ( (this->m_options & SymUseSymSrv) != 0)
973 {
974 if (GetEnvironmentVariableA("SYSTEMDRIVE", szTemp, nTempLen) > 0)
975 {
976 szTemp[nTempLen-1] = 0;
977 strcat_s(szSymPath, nSymPathLen, "SRV*");
978 strcat_s(szSymPath, nSymPathLen, szTemp);
979 strcat_s(szSymPath, nSymPathLen, "\\websymbols");
980 strcat_s(szSymPath, nSymPathLen, "*http://msdl.microsoft.com/download/symbols;");
981 }
982 else
983 strcat_s(szSymPath, nSymPathLen, "SRV*c:\\websymbols*http://msdl.microsoft.com/download/symbols;");
984 }
985 } // if SymBuildPath
986
987 // First Init the whole stuff...
988 BOOL bRet = this->m_sw->Init(szSymPath);
989 if (szSymPath != NULL) free(szSymPath); szSymPath = NULL;
990 if (bRet == FALSE)
991 {
992 this->OnDbgHelpErr("Error while initializing dbghelp.dll", 0, 0);
993 SetLastError(ERROR_DLL_INIT_FAILED);
994 return FALSE;
995 }
996
997 bRet = this->m_sw->LoadModules(this->m_hProcess, this->m_dwProcessId);
998 if (bRet != FALSE)
999 m_modulesLoaded = TRUE;
1000 return bRet;
1001 }
1002
1003
1004 // The following is used to pass the "userData"-Pointer to the user-provided readMemoryFunction
1005 // This has to be done due to a problem with the "hProcess"-parameter in x64...
1006 // Because this class is in no case multi-threading-enabled (because of the limitations
1007 // of dbghelp.dll) it is "safe" to use a static-variable
1008 static StackWalker::PReadProcessMemoryRoutine s_readMemoryFunction = NULL;
1009 static LPVOID s_readMemoryFunction_UserData = NULL;
1010
1011 BOOL StackWalker::ShowCallstack(HANDLE hThread, const CONTEXT *context, PReadProcessMemoryRoutine readMemoryFunction, LPVOID pUserData)
1012 {
1013 CONTEXT c;
1014 CallstackEntry csEntry;
1015 IMAGEHLP_SYMBOL64 *pSym = NULL;
1016 StackWalkerInternal::IMAGEHLP_MODULE64_V3 Module;
1017 IMAGEHLP_LINE64 Line;
1018 int frameNum;
1019 bool bLastEntryCalled = true;
1020 int curRecursionCount = 0;
1021
1022 if (m_modulesLoaded == FALSE)
1023 this->LoadModules(); // ignore the result...
1024
1025 if (this->m_sw->m_hDbhHelp == NULL)
1026 {
1027 SetLastError(ERROR_DLL_INIT_FAILED);
1028 return FALSE;
1029 }
1030
1031 s_readMemoryFunction = readMemoryFunction;
1032 s_readMemoryFunction_UserData = pUserData;
1033
1034 if (context == NULL)
1035 {
1036 // If no context is provided, capture the context
1037 // See: https://stackwalker.codeplex.com/discussions/446958
1038 #if _WIN32_WINNT <= 0x0501
1039 // If we need to support XP, we need to use the "old way", because "GetThreadId" is not available!
1040 if (hThread == GetCurrentThread())
1041 #else
1042 if (GetThreadId(hThread) == GetCurrentThreadId())
1043 #endif
1044 {
1045 GET_CURRENT_CONTEXT_STACKWALKER_CODEPLEX(c, USED_CONTEXT_FLAGS);
1046 }
1047 else
1048 {
1049 SuspendThread(hThread);
1050 memset(&c, 0, sizeof(CONTEXT));
1051 c.ContextFlags = USED_CONTEXT_FLAGS;
1052
1053 // TODO: Detect if you want to get a thread context of a different process, which is running a different processor architecture...
1054 // This does only work if we are x64 and the target process is x64 or x86;
1055 // It cannnot work, if this process is x64 and the target process is x64... this is not supported...
1056 // See also: http://www.howzatt.demon.co.uk/articles/DebuggingInWin64.html
1057 if (GetThreadContext(hThread, &c) == FALSE)
1058 {
1059 ResumeThread(hThread);
1060 return FALSE;
1061 }
1062 }
1063 }
1064 else
1065 c = *context;
1066
1067 // init STACKFRAME for first call
1068 STACKFRAME64 s; // in/out stackframe
1069 memset(&s, 0, sizeof(s));
1070 DWORD imageType;
1071 #ifdef _M_IX86
1072 // normally, call ImageNtHeader() and use machine info from PE header
1073 imageType = IMAGE_FILE_MACHINE_I386;
1074 s.AddrPC.Offset = c.Eip;
1075 s.AddrPC.Mode = AddrModeFlat;
1076 s.AddrFrame.Offset = c.Ebp;
1077 s.AddrFrame.Mode = AddrModeFlat;
1078 s.AddrStack.Offset = c.Esp;
1079 s.AddrStack.Mode = AddrModeFlat;
1080 #elif _M_X64
1081 imageType = IMAGE_FILE_MACHINE_AMD64;
1082 s.AddrPC.Offset = c.Rip;
1083 s.AddrPC.Mode = AddrModeFlat;
1084 s.AddrFrame.Offset = c.Rsp;
1085 s.AddrFrame.Mode = AddrModeFlat;
1086 s.AddrStack.Offset = c.Rsp;
1087 s.AddrStack.Mode = AddrModeFlat;
1088 #elif _M_IA64
1089 imageType = IMAGE_FILE_MACHINE_IA64;
1090 s.AddrPC.Offset = c.StIIP;
1091 s.AddrPC.Mode = AddrModeFlat;
1092 s.AddrFrame.Offset = c.IntSp;
1093 s.AddrFrame.Mode = AddrModeFlat;
1094 s.AddrBStore.Offset = c.RsBSP;
1095 s.AddrBStore.Mode = AddrModeFlat;
1096 s.AddrStack.Offset = c.IntSp;
1097 s.AddrStack.Mode = AddrModeFlat;
1098 #else
1099 #error "Platform not supported!"
1100 #endif
1101
1102 pSym = (IMAGEHLP_SYMBOL64 *) malloc(sizeof(IMAGEHLP_SYMBOL64) + STACKWALK_MAX_NAMELEN);
1103 if (!pSym) goto cleanup; // not enough memory...
1104 memset(pSym, 0, sizeof(IMAGEHLP_SYMBOL64) + STACKWALK_MAX_NAMELEN);
1105 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
1106 pSym->MaxNameLength = STACKWALK_MAX_NAMELEN;
1107
1108 memset(&Line, 0, sizeof(Line));
1109 Line.SizeOfStruct = sizeof(Line);
1110
1111 memset(&Module, 0, sizeof(Module));
1112 Module.SizeOfStruct = sizeof(Module);
1113
1114 for (frameNum = 0; ; ++frameNum )
1115 {
1116 // get next stack frame (StackWalk64(), SymFunctionTableAccess64(), SymGetModuleBase64())
1117 // if this returns ERROR_INVALID_ADDRESS (487) or ERROR_NOACCESS (998), you can
1118 // assume that either you are done, or that the stack is so hosed that the next
1119 // deeper frame could not be found.
1120 // CONTEXT need not to be suplied if imageTyp is IMAGE_FILE_MACHINE_I386!
1121 if ( ! this->m_sw->pSW(imageType, this->m_hProcess, hThread, &s, &c, myReadProcMem, this->m_sw->pSFTA, this->m_sw->pSGMB, NULL) )
1122 {
1123 // INFO: "StackWalk64" does not set "GetLastError"...
1124 this->OnDbgHelpErr("StackWalk64", 0, s.AddrPC.Offset);
1125 break;
1126 }
1127
1128 csEntry.offset = s.AddrPC.Offset;
1129 csEntry.name[0] = 0;
1130 csEntry.undName[0] = 0;
1131 csEntry.undFullName[0] = 0;
1132 csEntry.offsetFromSmybol = 0;
1133 csEntry.offsetFromLine = 0;
1134 csEntry.lineFileName[0] = 0;
1135 csEntry.lineNumber = 0;
1136 csEntry.loadedImageName[0] = 0;
1137 csEntry.moduleName[0] = 0;
1138 if (s.AddrPC.Offset == s.AddrReturn.Offset)
1139 {
1140 if ( (this->m_MaxRecursionCount > 0) && (curRecursionCount > m_MaxRecursionCount) )
1141 {
1142 this->OnDbgHelpErr("StackWalk64-Endless-Callstack!", 0, s.AddrPC.Offset);
1143 break;
1144 }
1145 curRecursionCount++;
1146 }
1147 else
1148 curRecursionCount = 0;
1149 if (s.AddrPC.Offset != 0)
1150 {
1151 // we seem to have a valid PC
1152 // show procedure info (SymGetSymFromAddr64())
1153 if (this->m_sw->pSGSFA(this->m_hProcess, s.AddrPC.Offset, &(csEntry.offsetFromSmybol), pSym) != FALSE)
1154 {
1155 MyStrCpy(csEntry.name, STACKWALK_MAX_NAMELEN, pSym->Name);
1156 // UnDecorateSymbolName()
1157 this->m_sw->pUDSN( pSym->Name, csEntry.undName, STACKWALK_MAX_NAMELEN, UNDNAME_NAME_ONLY );
1158 this->m_sw->pUDSN( pSym->Name, csEntry.undFullName, STACKWALK_MAX_NAMELEN, UNDNAME_COMPLETE );
1159 }
1160 else
1161 {
1162 this->OnDbgHelpErr("SymGetSymFromAddr64", GetLastError(), s.AddrPC.Offset);
1163 }
1164
1165 // show line number info, NT5.0-method (SymGetLineFromAddr64())
1166 if (this->m_sw->pSGLFA != NULL )
1167 { // yes, we have SymGetLineFromAddr64()
1168 if (this->m_sw->pSGLFA(this->m_hProcess, s.AddrPC.Offset, &(csEntry.offsetFromLine), &Line) != FALSE)
1169 {
1170 csEntry.lineNumber = Line.LineNumber;
1171 MyStrCpy(csEntry.lineFileName, STACKWALK_MAX_NAMELEN, Line.FileName);
1172 }
1173 else
1174 {
1175 this->OnDbgHelpErr("SymGetLineFromAddr64", GetLastError(), s.AddrPC.Offset);
1176 }
1177 } // yes, we have SymGetLineFromAddr64()
1178
1179 // show module info (SymGetModuleInfo64())
1180 if (this->m_sw->GetModuleInfo(this->m_hProcess, s.AddrPC.Offset, &Module ) != FALSE)
1181 { // got module info OK
1182 switch ( Module.SymType )
1183 {
1184 case SymNone:
1185 csEntry.symTypeString = "-nosymbols-";
1186 break;
1187 case SymCoff:
1188 csEntry.symTypeString = "COFF";
1189 break;
1190 case SymCv:
1191 csEntry.symTypeString = "CV";
1192 break;
1193 case SymPdb:
1194 csEntry.symTypeString = "PDB";
1195 break;
1196 case SymExport:
1197 csEntry.symTypeString = "-exported-";
1198 break;
1199 case SymDeferred:
1200 csEntry.symTypeString = "-deferred-";
1201 break;
1202 case SymSym:
1203 csEntry.symTypeString = "SYM";
1204 break;
1205 #if API_VERSION_NUMBER >= 9
1206 case SymDia:
1207 csEntry.symTypeString = "DIA";
1208 break;
1209 #endif
1210 case 8: //SymVirtual:
1211 csEntry.symTypeString = "Virtual";
1212 break;
1213 default:
1214 //_snprintf( ty, sizeof(ty), "symtype=%ld", (long) Module.SymType );
1215 csEntry.symTypeString = NULL;
1216 break;
1217 }
1218
1219 MyStrCpy(csEntry.moduleName, STACKWALK_MAX_NAMELEN, Module.ModuleName);
1220 csEntry.baseOfImage = Module.BaseOfImage;
1221 MyStrCpy(csEntry.loadedImageName, STACKWALK_MAX_NAMELEN, Module.LoadedImageName);
1222 } // got module info OK
1223 else
1224 {
1225 this->OnDbgHelpErr("SymGetModuleInfo64", GetLastError(), s.AddrPC.Offset);
1226 }
1227 } // we seem to have a valid PC
1228
1229 CallstackEntryType et = nextEntry;
1230 if (frameNum == 0)
1231 et = firstEntry;
1232 bLastEntryCalled = false;
1233 this->OnCallstackEntry(et, csEntry);
1234
1235 if (s.AddrReturn.Offset == 0)
1236 {
1237 bLastEntryCalled = true;
1238 this->OnCallstackEntry(lastEntry, csEntry);
1239 SetLastError(ERROR_SUCCESS);
1240 break;
1241 }
1242 } // for ( frameNum )
1243
1244 cleanup:
1245 if (pSym) free( pSym );
1246
1247 if (bLastEntryCalled == false)
1248 this->OnCallstackEntry(lastEntry, csEntry);
1249
1250 if (context == NULL)
1251 ResumeThread(hThread);
1252
1253 return TRUE;
1254 }
1255
1256 BOOL __stdcall StackWalker::myReadProcMem(
1257 HANDLE hProcess,
1258 DWORD64 qwBaseAddress,
1259 PVOID lpBuffer,
1260 DWORD nSize,
1261 LPDWORD lpNumberOfBytesRead
1262 )
1263 {
1264 if (s_readMemoryFunction == NULL)
1265 {
1266 SIZE_T st;
1267 BOOL bRet = ReadProcessMemory(hProcess, (LPVOID) qwBaseAddress, lpBuffer, nSize, &st);
1268 *lpNumberOfBytesRead = (DWORD) st;
1269 //printf("ReadMemory: hProcess: %p, baseAddr: %p, buffer: %p, size: %d, read: %d, result: %d\n", hProcess, (LPVOID) qwBaseAddress, lpBuffer, nSize, (DWORD) st, (DWORD) bRet);
1270 return bRet;
1271 }
1272 else
1273 {
1274 return s_readMemoryFunction(hProcess, qwBaseAddress, lpBuffer, nSize, lpNumberOfBytesRead, s_readMemoryFunction_UserData);
1275 }
1276 }
1277
1278 void StackWalker::OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion)
1279 {
1280 CHAR buffer[STACKWALK_MAX_NAMELEN];
1281 size_t maxLen = STACKWALK_MAX_NAMELEN;
1282 #if _MSC_VER >= 1400
1283 maxLen = _TRUNCATE;
1284 #endif
1285 if (fileVersion == 0)
1286 _snprintf_s(buffer, maxLen, "%s:%s (%p), size: %d (result: %d), SymType: '%s', PDB: '%s'\n", img, mod, (LPVOID)baseAddr, size, result, symType, pdbName);
1287 else
1288 {
1289 DWORD v4 = (DWORD) (fileVersion & 0xFFFF);
1290 DWORD v3 = (DWORD) ((fileVersion>>16) & 0xFFFF);
1291 DWORD v2 = (DWORD) ((fileVersion>>32) & 0xFFFF);
1292 DWORD v1 = (DWORD) ((fileVersion>>48) & 0xFFFF);
1293 _snprintf_s(buffer, maxLen, "%s:%s (%p), size: %d (result: %d), SymType: '%s', PDB: '%s', fileVersion: %d.%d.%d.%d\n", img, mod, (LPVOID) baseAddr, size, result, symType, pdbName, v1, v2, v3, v4);
1294 }
1295 buffer[STACKWALK_MAX_NAMELEN - 1] = 0; // be sure it is NUL terminated
1296 OnOutput(buffer);
1297 }
1298
1299 void StackWalker::OnCallstackEntry(CallstackEntryType eType, CallstackEntry &entry)
1300 {
1301 CHAR buffer[STACKWALK_MAX_NAMELEN];
1302 size_t maxLen = STACKWALK_MAX_NAMELEN;
1303 #if _MSC_VER >= 1400
1304 maxLen = _TRUNCATE;
1305 #endif
1306 if ( (eType != lastEntry) && (entry.offset != 0) )
1307 {
1308 if (entry.name[0] == 0)
1309 MyStrCpy(entry.name, STACKWALK_MAX_NAMELEN, "(function-name not available)");
1310 if (entry.undName[0] != 0)
1311 MyStrCpy(entry.name, STACKWALK_MAX_NAMELEN, entry.undName);
1312 if (entry.undFullName[0] != 0)
1313 MyStrCpy(entry.name, STACKWALK_MAX_NAMELEN, entry.undFullName);
1314 if (entry.lineFileName[0] == 0)
1315 {
1316 MyStrCpy(entry.lineFileName, STACKWALK_MAX_NAMELEN, "(filename not available)");
1317 if (entry.moduleName[0] == 0)
1318 MyStrCpy(entry.moduleName, STACKWALK_MAX_NAMELEN, "(module-name not available)");
1319 _snprintf_s(buffer, maxLen, "%p (%s): %s: %s\n", (LPVOID) entry.offset, entry.moduleName, entry.lineFileName, entry.name);
1320 }
1321 else
1322 _snprintf_s(buffer, maxLen, "%s (%d): %s\n", entry.lineFileName, entry.lineNumber, entry.name);
1323 buffer[STACKWALK_MAX_NAMELEN-1] = 0;
1324 OnOutput(buffer);
1325 }
1326 }
1327
1328 void StackWalker::OnDbgHelpErr(LPCSTR szFuncName, DWORD gle, DWORD64 addr)
1329 {
1330 CHAR buffer[STACKWALK_MAX_NAMELEN];
1331 size_t maxLen = STACKWALK_MAX_NAMELEN;
1332 #if _MSC_VER >= 1400
1333 maxLen = _TRUNCATE;
1334 #endif
1335 _snprintf_s(buffer, maxLen, "ERROR: %s, GetLastError: %d (Address: %p)\n", szFuncName, gle, (LPVOID) addr);
1336 buffer[STACKWALK_MAX_NAMELEN - 1] = 0;
1337 OnOutput(buffer);
1338 }
1339
1340 void StackWalker::OnSymInit(LPCSTR szSearchPath, DWORD symOptions, LPCSTR szUserName)
1341 {
1342 CHAR buffer[STACKWALK_MAX_NAMELEN];
1343 size_t maxLen = STACKWALK_MAX_NAMELEN;
1344 #if _MSC_VER >= 1400
1345 maxLen = _TRUNCATE;
1346 #endif
1347 _snprintf_s(buffer, maxLen, "SymInit: Symbol-SearchPath: '%s', symOptions: %d, UserName: '%s'\n", szSearchPath, symOptions, szUserName);
1348 buffer[STACKWALK_MAX_NAMELEN - 1] = 0;
1349 OnOutput(buffer);
1350 // Also display the OS-version
1351 #if _MSC_VER <= 1200
1352 OSVERSIONINFOA ver;
1353 ZeroMemory(&ver, sizeof(OSVERSIONINFOA));
1354 ver.dwOSVersionInfoSize = sizeof(ver);
1355 if (GetVersionExA(&ver) != FALSE)
1356 {
1357 _snprintf_s(buffer, maxLen, "OS-Version: %d.%d.%d (%s)\n",
1358 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
1359 ver.szCSDVersion);
1360 buffer[STACKWALK_MAX_NAMELEN - 1] = 0;
1361 OnOutput(buffer);
1362 }
1363 #else
1364 OSVERSIONINFOEXA ver;
1365 ZeroMemory(&ver, sizeof(OSVERSIONINFOEXA));
1366 ver.dwOSVersionInfoSize = sizeof(ver);
1367 #if _MSC_VER >= 1900
1368 #pragma warning(push)
1369 #pragma warning(disable: 4996)
1370 #endif
1371 if (GetVersionExA( (OSVERSIONINFOA*) &ver) != FALSE)
1372 {
1373 _snprintf_s(buffer, maxLen, "OS-Version: %d.%d.%d (%s) 0x%x-0x%x\n",
1374 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
1375 ver.szCSDVersion, ver.wSuiteMask, ver.wProductType);
1376 buffer[STACKWALK_MAX_NAMELEN - 1] = 0;
1377 OnOutput(buffer);
1378 }
1379 #if _MSC_VER >= 1900
1380 #pragma warning(pop)
1381 #endif
1382 #endif
1383 }
1384
1385 void StackWalker::OnOutput(LPCSTR buffer)
1386 {
1387 OutputDebugStringA(buffer);
1388 }
StackWalkerInternal
Definition: StackWalker.cc:253
TypeInfo
Definition: DataTypes.hh:129
StackWalkerInternal::tagMODULEENTRY32
Definition: StackWalker.cc:534
StackWalker
Definition: StackWalker.hh:56
StackWalkerInternal::IMAGEHLP_MODULE64_V3
Definition: StackWalker.cc:421
_IMAGEHLP_MODULE64
Definition: StackWalker.cc:119
_tagADDRESS64
Definition: StackWalker.cc:145
StackWalkerInternal::IMAGEHLP_MODULE64_V2
Definition: StackWalker.cc:449
StackWalkerInternal::_MODULEINFO
Definition: StackWalker.cc:613
_IMAGEHLP_SYMBOL64
Definition: StackWalker.cc:131
_tagSTACKFRAME64
Definition: StackWalker.cc:161
_IMAGEHLP_LINE64
Definition: StackWalker.cc:112
_KDHELP64
Definition: StackWalker.cc:150