423f308350 2010-11-07 kinaba: /************************************************************* 423f308350 2010-11-07 kinaba: Copyright (c) 2010 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: Benjamin Thaut. All rights reserved. 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: Redistribution and use in source and binary forms, with or without modification, are permitted 423f308350 2010-11-07 kinaba: provided that the following conditions are met: 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: 1. Redistributions of source code must retain the above copyright notice, this list of conditions 423f308350 2010-11-07 kinaba: and the following disclaimer. 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 423f308350 2010-11-07 kinaba: and the following disclaimer in the documentation and/or other materials provided with the distribution. 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: THIS SOFTWARE IS PROVIDED BY BENJAMIN THAUT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 423f308350 2010-11-07 kinaba: BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 423f308350 2010-11-07 kinaba: IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 423f308350 2010-11-07 kinaba: OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 423f308350 2010-11-07 kinaba: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 423f308350 2010-11-07 kinaba: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 423f308350 2010-11-07 kinaba: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: web: http://3d.benjamin-thaut.de 423f308350 2010-11-07 kinaba: *************************************************************/ 423f308350 2010-11-07 kinaba: module dbghelp; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: version (Windows){ 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: import std.c.windows.windows; 423f308350 2010-11-07 kinaba: import core.runtime; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: class Dbghelp { 423f308350 2010-11-07 kinaba: public: 423f308350 2010-11-07 kinaba: typedef char TCHAR; 423f308350 2010-11-07 kinaba: typedef ulong DWORD64; 423f308350 2010-11-07 kinaba: typedef char* CTSTR; 423f308350 2010-11-07 kinaba: typedef char* PTSTR; 423f308350 2010-11-07 kinaba: typedef const(char)* PCSTR; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: enum ADDRESS_MODE : DWORD { 423f308350 2010-11-07 kinaba: AddrMode1616 = 0, 423f308350 2010-11-07 kinaba: AddrMode1632 = 1, 423f308350 2010-11-07 kinaba: AddrModeReal = 2, 423f308350 2010-11-07 kinaba: AddrModeFlat = 3 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: enum : DWORD { 423f308350 2010-11-07 kinaba: SYMOPT_FAIL_CRITICAL_ERRORS = 0x00000200, 423f308350 2010-11-07 kinaba: SYMOPT_LOAD_LINES = 0x00000010 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: struct GUID { 423f308350 2010-11-07 kinaba: uint Data1; 423f308350 2010-11-07 kinaba: ushort Data2; 423f308350 2010-11-07 kinaba: ushort Data3; 423f308350 2010-11-07 kinaba: ubyte[8] Data4; 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: struct ADDRESS64 { 423f308350 2010-11-07 kinaba: DWORD64 Offset; 423f308350 2010-11-07 kinaba: WORD Segment; 423f308350 2010-11-07 kinaba: ADDRESS_MODE Mode; 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: struct KDHELP64 { 423f308350 2010-11-07 kinaba: DWORD64 Thread; 423f308350 2010-11-07 kinaba: DWORD ThCallbackStack; 423f308350 2010-11-07 kinaba: DWORD ThCallbackBStore; 423f308350 2010-11-07 kinaba: DWORD NextCallback; 423f308350 2010-11-07 kinaba: DWORD FramePointer; 423f308350 2010-11-07 kinaba: DWORD64 KiCallUserMode; 423f308350 2010-11-07 kinaba: DWORD64 KeUserCallbackDispatcher; 423f308350 2010-11-07 kinaba: DWORD64 SystemRangeStart; 423f308350 2010-11-07 kinaba: DWORD64 KiUserExceptionDispatcher; 423f308350 2010-11-07 kinaba: DWORD64[7] Reserved; 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: struct STACKFRAME64 { 423f308350 2010-11-07 kinaba: ADDRESS64 AddrPC; 423f308350 2010-11-07 kinaba: ADDRESS64 AddrReturn; 423f308350 2010-11-07 kinaba: ADDRESS64 AddrFrame; 423f308350 2010-11-07 kinaba: ADDRESS64 AddrStack; 423f308350 2010-11-07 kinaba: ADDRESS64 AddrBStore; 423f308350 2010-11-07 kinaba: PVOID FuncTableEntry; 423f308350 2010-11-07 kinaba: DWORD64[4] Params; 423f308350 2010-11-07 kinaba: BOOL Far; 423f308350 2010-11-07 kinaba: BOOL Virtual; 423f308350 2010-11-07 kinaba: DWORD64[3] Reserved; 423f308350 2010-11-07 kinaba: KDHELP64 KdHelp; 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: enum : DWORD { 423f308350 2010-11-07 kinaba: IMAGE_FILE_MACHINE_I386 = 0x014c, 423f308350 2010-11-07 kinaba: IMGAE_FILE_MACHINE_IA64 = 0x0200, 423f308350 2010-11-07 kinaba: IMAGE_FILE_MACHINE_AMD64 = 0x8664 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: struct IMAGEHLP_LINE64 { 423f308350 2010-11-07 kinaba: DWORD SizeOfStruct; 423f308350 2010-11-07 kinaba: PVOID Key; 423f308350 2010-11-07 kinaba: DWORD LineNumber; 423f308350 2010-11-07 kinaba: PTSTR FileName; 423f308350 2010-11-07 kinaba: DWORD64 Address; 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: enum SYM_TYPE : int { 423f308350 2010-11-07 kinaba: SymNone = 0, 423f308350 2010-11-07 kinaba: SymCoff, 423f308350 2010-11-07 kinaba: SymCv, 423f308350 2010-11-07 kinaba: SymPdb, 423f308350 2010-11-07 kinaba: SymExport, 423f308350 2010-11-07 kinaba: SymDeferred, 423f308350 2010-11-07 kinaba: SymSym, 423f308350 2010-11-07 kinaba: SymDia, 423f308350 2010-11-07 kinaba: SymVirtual, 423f308350 2010-11-07 kinaba: NumSymTypes 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: struct IMAGEHLP_MODULE64 { 423f308350 2010-11-07 kinaba: DWORD SizeOfStruct; 423f308350 2010-11-07 kinaba: DWORD64 BaseOfImage; 423f308350 2010-11-07 kinaba: DWORD ImageSize; 423f308350 2010-11-07 kinaba: DWORD TimeDateStamp; 423f308350 2010-11-07 kinaba: DWORD CheckSum; 423f308350 2010-11-07 kinaba: DWORD NumSyms; 423f308350 2010-11-07 kinaba: SYM_TYPE SymType; 423f308350 2010-11-07 kinaba: TCHAR[32] ModuleName; 423f308350 2010-11-07 kinaba: TCHAR[256] ImageName; 423f308350 2010-11-07 kinaba: TCHAR[256] LoadedImageName; 423f308350 2010-11-07 kinaba: TCHAR[256] LoadedPdbName; 423f308350 2010-11-07 kinaba: DWORD CVSig; 423f308350 2010-11-07 kinaba: TCHAR[MAX_PATH*3] CVData; 423f308350 2010-11-07 kinaba: DWORD PdbSig; 423f308350 2010-11-07 kinaba: GUID PdbSig70; 423f308350 2010-11-07 kinaba: DWORD PdbAge; 423f308350 2010-11-07 kinaba: BOOL PdbUnmatched; 423f308350 2010-11-07 kinaba: BOOL DbgUnmachted; 423f308350 2010-11-07 kinaba: BOOL LineNumbers; 423f308350 2010-11-07 kinaba: BOOL GlobalSymbols; 423f308350 2010-11-07 kinaba: BOOL TypeInfo; 423f308350 2010-11-07 kinaba: BOOL SourceIndexed; 423f308350 2010-11-07 kinaba: BOOL Publics; 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: struct IMAGEHLP_SYMBOL64 { 423f308350 2010-11-07 kinaba: DWORD SizeOfStruct; 423f308350 2010-11-07 kinaba: DWORD64 Address; 423f308350 2010-11-07 kinaba: DWORD Size; 423f308350 2010-11-07 kinaba: DWORD Flags; 423f308350 2010-11-07 kinaba: DWORD MaxNameLength; 423f308350 2010-11-07 kinaba: TCHAR[1] Name; 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: extern(System){ 423f308350 2010-11-07 kinaba: typedef BOOL function(HANDLE hProcess, DWORD64 lpBaseAddress, PVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead) ReadProcessMemoryProc64; 423f308350 2010-11-07 kinaba: typedef PVOID function(HANDLE hProcess, DWORD64 AddrBase) FunctionTableAccessProc64; 423f308350 2010-11-07 kinaba: typedef DWORD64 function(HANDLE hProcess, DWORD64 Address) GetModuleBaseProc64; 423f308350 2010-11-07 kinaba: typedef DWORD64 function(HANDLE hProcess, HANDLE hThread, ADDRESS64 *lpaddr) TranslateAddressProc64; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: typedef BOOL function(HANDLE hProcess, PCSTR UserSearchPath, bool fInvadeProcess) SymInitializeFunc; 423f308350 2010-11-07 kinaba: typedef BOOL function(HANDLE hProcess) SymCleanupFunc; 423f308350 2010-11-07 kinaba: typedef DWORD function(DWORD SymOptions) SymSetOptionsFunc; 423f308350 2010-11-07 kinaba: typedef DWORD function() SymGetOptionsFunc; 423f308350 2010-11-07 kinaba: typedef PVOID function(HANDLE hProcess, DWORD64 AddrBase) SymFunctionTableAccess64Func; 423f308350 2010-11-07 kinaba: typedef BOOL function(DWORD MachineType, HANDLE hProcess, HANDLE hThread, STACKFRAME64 *StackFrame, PVOID ContextRecord, 423f308350 2010-11-07 kinaba: ReadProcessMemoryProc64 ReadMemoryRoutine, FunctionTableAccessProc64 FunctoinTableAccess, 423f308350 2010-11-07 kinaba: GetModuleBaseProc64 GetModuleBaseRoutine, TranslateAddressProc64 TranslateAddress) StackWalk64Func; 423f308350 2010-11-07 kinaba: typedef BOOL function(HANDLE hProcess, DWORD64 dwAddr, PDWORD pdwDisplacement, IMAGEHLP_LINE64 *line) SymGetLineFromAddr64Func; 423f308350 2010-11-07 kinaba: typedef DWORD64 function(HANDLE hProcess, DWORD64 dwAddr) SymGetModuleBase64Func; 423f308350 2010-11-07 kinaba: typedef BOOL function(HANDLE hProcess, DWORD64 dwAddr, IMAGEHLP_MODULE64 *ModuleInfo) SymGetModuleInfo64Func; 423f308350 2010-11-07 kinaba: typedef BOOL function(HANDLE hProcess, DWORD64 Address, DWORD64 *Displacement, IMAGEHLP_SYMBOL64 *Symbol) SymGetSymFromAddr64Func; 423f308350 2010-11-07 kinaba: typedef DWORD function(CTSTR *DecoratedName, PTSTR UnDecoratedName, DWORD UndecoratedLength, DWORD Flags) UnDecorateSymbolNameFunc; 423f308350 2010-11-07 kinaba: typedef DWORD64 function(HANDLE hProcess, HANDLE hFile, PCSTR ImageName, PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll) SymLoadModule64Func; 423f308350 2010-11-07 kinaba: typedef BOOL function(HANDLE HProcess, PTSTR SearchPath, DWORD SearchPathLength) SymGetSearchPathFunc; 423f308350 2010-11-07 kinaba: typedef BOOL function(HANDLE hProcess, DWORD64 Address) SymUnloadModule64Func; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: private static bool isInit = false; 423f308350 2010-11-07 kinaba: private static HANDLE dbghelp_lib = cast(HANDLE)null; 423f308350 2010-11-07 kinaba: static SymInitializeFunc SymInitialize; 423f308350 2010-11-07 kinaba: static SymCleanupFunc SymCleanup; 423f308350 2010-11-07 kinaba: static StackWalk64Func StackWalk64; 423f308350 2010-11-07 kinaba: static SymGetOptionsFunc SymGetOptions; 423f308350 2010-11-07 kinaba: static SymSetOptionsFunc SymSetOptions; 423f308350 2010-11-07 kinaba: static SymFunctionTableAccess64Func SymFunctionTableAccess64; 423f308350 2010-11-07 kinaba: static SymGetLineFromAddr64Func SymGetLineFromAddr64; 423f308350 2010-11-07 kinaba: static SymGetModuleBase64Func SymGetModuleBase64; 423f308350 2010-11-07 kinaba: static SymGetModuleInfo64Func SymGetModuleInfo64; 423f308350 2010-11-07 kinaba: static SymGetSymFromAddr64Func SymGetSymFromAddr64; 423f308350 2010-11-07 kinaba: static UnDecorateSymbolNameFunc UnDecorateSymbolName; 423f308350 2010-11-07 kinaba: static SymLoadModule64Func SymLoadModule64; 423f308350 2010-11-07 kinaba: static SymGetSearchPathFunc SymGetSearchPath; 423f308350 2010-11-07 kinaba: static SymUnloadModule64Func SymUnloadModule64; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: static bool Init(){ 423f308350 2010-11-07 kinaba: if(isInit) 423f308350 2010-11-07 kinaba: return true; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: dbghelp_lib = cast(HANDLE)Runtime.loadLibrary("dbghelp.dll"); 423f308350 2010-11-07 kinaba: if(dbghelp_lib == null) 423f308350 2010-11-07 kinaba: return false; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: SymInitialize = cast(SymInitializeFunc) GetProcAddress(dbghelp_lib,"SymInitialize"); 423f308350 2010-11-07 kinaba: SymCleanup = cast(SymCleanupFunc) GetProcAddress(dbghelp_lib,"SymCleanup"); 423f308350 2010-11-07 kinaba: StackWalk64 = cast(StackWalk64Func) GetProcAddress(dbghelp_lib,"StackWalk64"); 423f308350 2010-11-07 kinaba: SymGetOptions = cast(SymGetOptionsFunc) GetProcAddress(dbghelp_lib,"SymGetOptions"); 423f308350 2010-11-07 kinaba: SymSetOptions = cast(SymSetOptionsFunc) GetProcAddress(dbghelp_lib,"SymSetOptions"); 423f308350 2010-11-07 kinaba: SymFunctionTableAccess64 = cast(SymFunctionTableAccess64Func) GetProcAddress(dbghelp_lib,"SymFunctionTableAccess64"); 423f308350 2010-11-07 kinaba: SymGetLineFromAddr64 = cast(SymGetLineFromAddr64Func) GetProcAddress(dbghelp_lib,"SymGetLineFromAddr64"); 423f308350 2010-11-07 kinaba: SymGetModuleBase64 = cast(SymGetModuleBase64Func) GetProcAddress(dbghelp_lib,"SymGetModuleBase64"); 423f308350 2010-11-07 kinaba: SymGetModuleInfo64 = cast(SymGetModuleInfo64Func) GetProcAddress(dbghelp_lib,"SymGetModuleInfo64"); 423f308350 2010-11-07 kinaba: SymGetSymFromAddr64 = cast(SymGetSymFromAddr64Func) GetProcAddress(dbghelp_lib,"SymGetSymFromAddr64"); 423f308350 2010-11-07 kinaba: SymLoadModule64 = cast(SymLoadModule64Func) GetProcAddress(dbghelp_lib,"SymLoadModule64"); 423f308350 2010-11-07 kinaba: SymGetSearchPath = cast(SymGetSearchPathFunc) GetProcAddress(dbghelp_lib,"SymGetSearchPath"); 423f308350 2010-11-07 kinaba: SymUnloadModule64 = cast(SymUnloadModule64Func) GetProcAddress(dbghelp_lib,"SymUnloadModule64"); 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: if(!SymInitialize || !SymCleanup || !StackWalk64 || !SymGetOptions || !SymSetOptions || !SymFunctionTableAccess64 423f308350 2010-11-07 kinaba: || !SymGetLineFromAddr64 || !SymGetModuleBase64 || !SymGetModuleInfo64 || !SymGetSymFromAddr64 423f308350 2010-11-07 kinaba: || !SymLoadModule64 || !SymGetSearchPath || !SymUnloadModule64){ 423f308350 2010-11-07 kinaba: return false; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: isInit = true; 423f308350 2010-11-07 kinaba: return true; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: void DeInit(){ 423f308350 2010-11-07 kinaba: if(isInit){ 423f308350 2010-11-07 kinaba: Runtime.unloadLibrary(dbghelp_lib); 423f308350 2010-11-07 kinaba: isInit = false; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: }; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: }