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 @system:
15 
16 import core.sys.windows.winbase /+: FreeLibrary, GetProcAddress, LoadLibraryA+/;
17 import core.sys.windows.windef;
18 
19 public import core.sys.windows.dbghelp_types;
20 
21 extern(Windows)
22 {
23     alias BOOL         function(HANDLE hProcess, DWORD64 lpBaseAddress, PVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead) ReadProcessMemoryProc64;
24     alias PVOID        function(HANDLE hProcess, DWORD64 AddrBase) FunctionTableAccessProc64;
25     alias DWORD64      function(HANDLE hProcess, DWORD64 Address) GetModuleBaseProc64;
26     alias DWORD64      function(HANDLE hProcess, HANDLE hThread, ADDRESS64 *lpaddr) TranslateAddressProc64;
27 
28     alias BOOL         function(HANDLE hProcess, PCSTR UserSearchPath, bool fInvadeProcess) SymInitializeFunc;
29     alias BOOL         function(HANDLE hProcess) SymCleanupFunc;
30     alias DWORD        function(DWORD SymOptions) SymSetOptionsFunc;
31     alias DWORD        function() SymGetOptionsFunc;
32     alias PVOID        function(HANDLE hProcess, DWORD64 AddrBase) SymFunctionTableAccess64Func;
33     alias BOOL         function(DWORD MachineType, HANDLE hProcess, HANDLE hThread, STACKFRAME64 *StackFrame, PVOID ContextRecord,
34                                 ReadProcessMemoryProc64 ReadMemoryRoutine, FunctionTableAccessProc64 FunctoinTableAccess,
35                                 GetModuleBaseProc64 GetModuleBaseRoutine, TranslateAddressProc64 TranslateAddress) StackWalk64Func;
36     alias BOOL         function(HANDLE hProcess, DWORD64 dwAddr, PDWORD pdwDisplacement, IMAGEHLP_LINEA64 *line) SymGetLineFromAddr64Func;
37     alias DWORD64      function(HANDLE hProcess, DWORD64 dwAddr) SymGetModuleBase64Func;
38     alias BOOL         function(HANDLE hProcess, DWORD64 dwAddr, IMAGEHLP_MODULEA64 *ModuleInfo) SymGetModuleInfo64Func;
39     alias BOOL         function(HANDLE hProcess, DWORD64 Address, DWORD64 *Displacement, IMAGEHLP_SYMBOLA64 *Symbol) SymGetSymFromAddr64Func;
40     alias DWORD        function(PCSTR DecoratedName, PSTR UnDecoratedName, DWORD UndecoratedLength, DWORD Flags) UnDecorateSymbolNameFunc;
41     alias DWORD64      function(HANDLE hProcess, HANDLE hFile, PCSTR ImageName, PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll) SymLoadModule64Func;
42     alias BOOL         function(HANDLE HProcess, PTSTR SearchPath, DWORD SearchPathLength) SymGetSearchPathFunc;
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     SymUnloadModule64Func    SymUnloadModule64;
65     SymRegisterCallback64Func SymRegisterCallback64;
66     ImagehlpApiVersionFunc   ImagehlpApiVersion;
67 
getDbgHelp68     static DbgHelp* get()
69     {
70         if ( sm_hndl != sm_hndl.init )
71             return &sm_inst;
72         if ( (sm_hndl = LoadLibraryA( "dbghelp.dll" )) != sm_hndl.init )
73         {
74             sm_inst.SymInitialize            = cast(SymInitializeFunc) GetProcAddress(sm_hndl,"SymInitialize");
75             sm_inst.SymCleanup               = cast(SymCleanupFunc) GetProcAddress(sm_hndl,"SymCleanup");
76             sm_inst.StackWalk64              = cast(StackWalk64Func) GetProcAddress(sm_hndl,"StackWalk64");
77             sm_inst.SymGetOptions            = cast(SymGetOptionsFunc) GetProcAddress(sm_hndl,"SymGetOptions");
78             sm_inst.SymSetOptions            = cast(SymSetOptionsFunc) GetProcAddress(sm_hndl,"SymSetOptions");
79             sm_inst.SymFunctionTableAccess64 = cast(SymFunctionTableAccess64Func) GetProcAddress(sm_hndl,"SymFunctionTableAccess64");
80             sm_inst.SymGetLineFromAddr64     = cast(SymGetLineFromAddr64Func) GetProcAddress(sm_hndl,"SymGetLineFromAddr64");
81             sm_inst.SymGetModuleBase64       = cast(SymGetModuleBase64Func) GetProcAddress(sm_hndl,"SymGetModuleBase64");
82             sm_inst.SymGetModuleInfo64       = cast(SymGetModuleInfo64Func) GetProcAddress(sm_hndl,"SymGetModuleInfo64");
83             sm_inst.SymGetSymFromAddr64      = cast(SymGetSymFromAddr64Func) GetProcAddress(sm_hndl,"SymGetSymFromAddr64");
84             sm_inst.UnDecorateSymbolName     = cast(UnDecorateSymbolNameFunc) GetProcAddress(sm_hndl,"UnDecorateSymbolName");
85             sm_inst.SymLoadModule64          = cast(SymLoadModule64Func) GetProcAddress(sm_hndl,"SymLoadModule64");
86             sm_inst.SymGetSearchPath         = cast(SymGetSearchPathFunc) GetProcAddress(sm_hndl,"SymGetSearchPath");
87             sm_inst.SymUnloadModule64        = cast(SymUnloadModule64Func) GetProcAddress(sm_hndl,"SymUnloadModule64");
88             sm_inst.SymRegisterCallback64    = cast(SymRegisterCallback64Func) GetProcAddress(sm_hndl, "SymRegisterCallback64");
89             sm_inst.ImagehlpApiVersion       = cast(ImagehlpApiVersionFunc) GetProcAddress(sm_hndl, "ImagehlpApiVersion");
90             assert( sm_inst.SymInitialize && sm_inst.SymCleanup && sm_inst.StackWalk64 && sm_inst.SymGetOptions &&
91                     sm_inst.SymSetOptions && sm_inst.SymFunctionTableAccess64 && sm_inst.SymGetLineFromAddr64 &&
92                     sm_inst.SymGetModuleBase64 && sm_inst.SymGetModuleInfo64 && sm_inst.SymGetSymFromAddr64 &&
93                     sm_inst.UnDecorateSymbolName && sm_inst.SymLoadModule64 && sm_inst.SymGetSearchPath &&
94                     sm_inst.SymUnloadModule64 && sm_inst.SymRegisterCallback64 && sm_inst.ImagehlpApiVersion);
95 
96             return &sm_inst;
97         }
98         return null;
99     }
100 
~thisDbgHelp101     shared static ~this()
102     {
103         if ( sm_hndl != sm_hndl.init )
104             FreeLibrary( sm_hndl );
105     }
106 
107 private:
108     __gshared DbgHelp sm_inst;
109     __gshared HANDLE  sm_hndl;
110 }
111