1 /** 2 * ... 3 * 4 * Copyright: Copyright Benjamin Thaut 2010 - 2011. 5 * License: Distributed under the 6 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). 7 * (See accompanying file LICENSE) 8 * Authors: Benjamin Thaut, Sean Kelly 9 * Source: $(DRUNTIMESRC core/sys/windows/_stacktrace.d) 10 */ 11 12 module core.sys.windows.dbghelp; 13 version (Windows): 14 15 import core.sys.windows.winbase /+: FreeLibrary, GetProcAddress, LoadLibraryA+/; 16 import core.sys.windows.windef; 17 18 public import core.sys.windows.dbghelp_types; 19 20 extern(Windows) 21 { 22 alias BOOL function(HANDLE hProcess, DWORD64 lpBaseAddress, PVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead) ReadProcessMemoryProc64; 23 alias PVOID function(HANDLE hProcess, DWORD64 AddrBase) FunctionTableAccessProc64; 24 alias DWORD64 function(HANDLE hProcess, DWORD64 Address) GetModuleBaseProc64; 25 alias DWORD64 function(HANDLE hProcess, HANDLE hThread, ADDRESS64 *lpaddr) TranslateAddressProc64; 26 27 alias BOOL function(HANDLE hProcess, PCSTR UserSearchPath, bool fInvadeProcess) SymInitializeFunc; 28 alias BOOL function(HANDLE hProcess) SymCleanupFunc; 29 alias DWORD function(DWORD SymOptions) SymSetOptionsFunc; 30 alias DWORD function() SymGetOptionsFunc; 31 alias PVOID function(HANDLE hProcess, DWORD64 AddrBase) SymFunctionTableAccess64Func; 32 alias BOOL function(DWORD MachineType, HANDLE hProcess, HANDLE hThread, STACKFRAME64 *StackFrame, PVOID ContextRecord, 33 ReadProcessMemoryProc64 ReadMemoryRoutine, FunctionTableAccessProc64 FunctoinTableAccess, 34 GetModuleBaseProc64 GetModuleBaseRoutine, TranslateAddressProc64 TranslateAddress) @nogc StackWalk64Func; 35 alias BOOL function(HANDLE hProcess, DWORD64 dwAddr, PDWORD pdwDisplacement, IMAGEHLP_LINEA64 *line) SymGetLineFromAddr64Func; 36 alias DWORD64 function(HANDLE hProcess, DWORD64 dwAddr) SymGetModuleBase64Func; 37 alias BOOL function(HANDLE hProcess, DWORD64 dwAddr, IMAGEHLP_MODULEA64 *ModuleInfo) SymGetModuleInfo64Func; 38 alias BOOL function(HANDLE hProcess, DWORD64 Address, DWORD64 *Displacement, IMAGEHLP_SYMBOLA64 *Symbol) SymGetSymFromAddr64Func; 39 alias DWORD function(PCSTR DecoratedName, PSTR UnDecoratedName, DWORD UndecoratedLength, DWORD Flags) UnDecorateSymbolNameFunc; 40 alias DWORD64 function(HANDLE hProcess, HANDLE hFile, PCSTR ImageName, PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll) SymLoadModule64Func; 41 alias BOOL function(HANDLE hProcess, PSTR SearchPath, DWORD SearchPathLength) SymGetSearchPathFunc; 42 alias BOOL function(HANDLE hProcess, PCSTR SearchPath) SymSetSearchPathFunc; 43 alias BOOL function(HANDLE hProcess, DWORD64 Address) SymUnloadModule64Func; 44 alias BOOL function(HANDLE hProcess, ULONG ActionCode, ulong CallbackContext, ulong UserContext) PSYMBOL_REGISTERED_CALLBACK64; 45 alias BOOL function(HANDLE hProcess, PSYMBOL_REGISTERED_CALLBACK64 CallbackFunction, ulong UserContext) SymRegisterCallback64Func; 46 alias API_VERSION* function() ImagehlpApiVersionFunc; 47 } 48 49 struct DbgHelp 50 { 51 SymInitializeFunc SymInitialize; 52 SymCleanupFunc SymCleanup; 53 StackWalk64Func StackWalk64; 54 SymGetOptionsFunc SymGetOptions; 55 SymSetOptionsFunc SymSetOptions; 56 SymFunctionTableAccess64Func SymFunctionTableAccess64; 57 SymGetLineFromAddr64Func SymGetLineFromAddr64; 58 SymGetModuleBase64Func SymGetModuleBase64; 59 SymGetModuleInfo64Func SymGetModuleInfo64; 60 SymGetSymFromAddr64Func SymGetSymFromAddr64; 61 UnDecorateSymbolNameFunc UnDecorateSymbolName; 62 SymLoadModule64Func SymLoadModule64; 63 SymGetSearchPathFunc SymGetSearchPath; 64 SymSetSearchPathFunc SymSetSearchPath; 65 SymUnloadModule64Func SymUnloadModule64; 66 SymRegisterCallback64Func SymRegisterCallback64; 67 ImagehlpApiVersionFunc ImagehlpApiVersion; 68 69 static DbgHelp* get() @nogc 70 { 71 if ( sm_hndl != sm_hndl.init ) 72 return &sm_inst; 73 if ( (sm_hndl = LoadLibraryA( "dbghelp.dll" )) != sm_hndl.init ) 74 { 75 sm_inst.SymInitialize = cast(SymInitializeFunc) GetProcAddress(sm_hndl,"SymInitialize"); 76 sm_inst.SymCleanup = cast(SymCleanupFunc) GetProcAddress(sm_hndl,"SymCleanup"); 77 sm_inst.StackWalk64 = cast(StackWalk64Func) GetProcAddress(sm_hndl,"StackWalk64"); 78 sm_inst.SymGetOptions = cast(SymGetOptionsFunc) GetProcAddress(sm_hndl,"SymGetOptions"); 79 sm_inst.SymSetOptions = cast(SymSetOptionsFunc) GetProcAddress(sm_hndl,"SymSetOptions"); 80 sm_inst.SymFunctionTableAccess64 = cast(SymFunctionTableAccess64Func) GetProcAddress(sm_hndl,"SymFunctionTableAccess64"); 81 sm_inst.SymGetLineFromAddr64 = cast(SymGetLineFromAddr64Func) GetProcAddress(sm_hndl,"SymGetLineFromAddr64"); 82 sm_inst.SymGetModuleBase64 = cast(SymGetModuleBase64Func) GetProcAddress(sm_hndl,"SymGetModuleBase64"); 83 sm_inst.SymGetModuleInfo64 = cast(SymGetModuleInfo64Func) GetProcAddress(sm_hndl,"SymGetModuleInfo64"); 84 sm_inst.SymGetSymFromAddr64 = cast(SymGetSymFromAddr64Func) GetProcAddress(sm_hndl,"SymGetSymFromAddr64"); 85 sm_inst.UnDecorateSymbolName = cast(UnDecorateSymbolNameFunc) GetProcAddress(sm_hndl,"UnDecorateSymbolName"); 86 sm_inst.SymLoadModule64 = cast(SymLoadModule64Func) GetProcAddress(sm_hndl,"SymLoadModule64"); 87 sm_inst.SymGetSearchPath = cast(SymGetSearchPathFunc) GetProcAddress(sm_hndl,"SymGetSearchPath"); 88 sm_inst.SymSetSearchPath = cast(SymSetSearchPathFunc) GetProcAddress(sm_hndl,"SymSetSearchPath"); 89 sm_inst.SymUnloadModule64 = cast(SymUnloadModule64Func) GetProcAddress(sm_hndl,"SymUnloadModule64"); 90 sm_inst.SymRegisterCallback64 = cast(SymRegisterCallback64Func) GetProcAddress(sm_hndl, "SymRegisterCallback64"); 91 sm_inst.ImagehlpApiVersion = cast(ImagehlpApiVersionFunc) GetProcAddress(sm_hndl, "ImagehlpApiVersion"); 92 assert( sm_inst.SymInitialize && sm_inst.SymCleanup && sm_inst.StackWalk64 && sm_inst.SymGetOptions && 93 sm_inst.SymSetOptions && sm_inst.SymFunctionTableAccess64 && sm_inst.SymGetLineFromAddr64 && 94 sm_inst.SymGetModuleBase64 && sm_inst.SymGetModuleInfo64 && sm_inst.SymGetSymFromAddr64 && 95 sm_inst.UnDecorateSymbolName && sm_inst.SymLoadModule64 && sm_inst.SymGetSearchPath && 96 sm_inst.SymSetSearchPath && sm_inst.SymUnloadModule64 && sm_inst.SymRegisterCallback64 && 97 sm_inst.ImagehlpApiVersion); 98 99 return &sm_inst; 100 } 101 return null; 102 } 103 104 shared static ~this() 105 { 106 if ( sm_hndl != sm_hndl.init ) 107 FreeLibrary( sm_hndl ); 108 } 109 110 private: 111 __gshared DbgHelp sm_inst; 112 __gshared HANDLE sm_hndl; 113 }