|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* Win32 x86/x64 code for stack walking, symbol resolution, and function hooking */ |
|
7 |
|
8 #ifndef __nsDebugHelpWin32_h__ |
|
9 #define __nsDebugHelpWin32_h__ |
|
10 |
|
11 #if defined(_WIN32) && (defined(_M_IX86) || defined(_M_X64)) |
|
12 #ifndef WIN32_LEAN_AND_MEAN |
|
13 #define WIN32_LEAN_AND_MEAN |
|
14 #endif |
|
15 #include <windows.h> |
|
16 #include <imagehlp.h> |
|
17 #include <crtdbg.h> |
|
18 #else |
|
19 #error "nsDebugHelpWin32.h should only be included in Win32 x86/x64 builds" |
|
20 #endif |
|
21 |
|
22 // XXX temporary hack... |
|
23 //#include "hacky_defines.h" |
|
24 |
|
25 |
|
26 /***************************************************************************/ |
|
27 // useful macros... |
|
28 |
|
29 #ifdef DHW_IMPLEMENT_GLOBALS |
|
30 #define DHW_DECLARE_FUN_GLOBAL(name_) decltype(name_)* dhw##name_ |
|
31 #else |
|
32 #define DHW_DECLARE_FUN_GLOBAL(name_) extern decltype(name_)* dhw##name_ |
|
33 #endif |
|
34 |
|
35 |
|
36 /**********************************************************/ |
|
37 // This is used to get 'original' function addresses from DHWImportHooker. |
|
38 |
|
39 #define DHW_ORIGINAL(name_, hooker_) \ |
|
40 ((decltype(name_)*) hooker_ . GetOriginalFunction()) |
|
41 |
|
42 /***************************************************************************/ |
|
43 // Global declarations of entry points into ImgHelp functions |
|
44 |
|
45 #ifndef _WIN64 |
|
46 DHW_DECLARE_FUN_GLOBAL(EnumerateLoadedModules); |
|
47 #else |
|
48 DHW_DECLARE_FUN_GLOBAL(EnumerateLoadedModules64); |
|
49 #endif |
|
50 |
|
51 DHW_DECLARE_FUN_GLOBAL(ImageDirectoryEntryToData); |
|
52 |
|
53 /***************************************************************************/ |
|
54 |
|
55 extern bool |
|
56 dhwEnsureImageHlpInitialized(); |
|
57 |
|
58 /***************************************************************************/ |
|
59 |
|
60 class DHWImportHooker |
|
61 { |
|
62 public: |
|
63 |
|
64 DHWImportHooker(const char* aModuleName, |
|
65 const char* aFunctionName, |
|
66 PROC aHook, |
|
67 bool aExcludeOurModule = false); |
|
68 |
|
69 ~DHWImportHooker(); |
|
70 |
|
71 PROC GetOriginalFunction() {return mOriginal;} |
|
72 |
|
73 bool PatchAllModules(); |
|
74 bool PatchOneModule(HMODULE aModule, const char* name); |
|
75 static bool ModuleLoaded(HMODULE aModule, DWORD flags); |
|
76 |
|
77 |
|
78 // I think that these should be made not static members, but allocated |
|
79 // things created in an explicit static 'init' method and cleaned up in |
|
80 // an explicit static 'finish' method. This would allow the application |
|
81 // to have proper lifetime control over all the hooks. |
|
82 |
|
83 static DHWImportHooker &getLoadLibraryWHooker(); |
|
84 static DHWImportHooker &getLoadLibraryExWHooker(); |
|
85 static DHWImportHooker &getLoadLibraryAHooker(); |
|
86 static DHWImportHooker &getLoadLibraryExAHooker(); |
|
87 static DHWImportHooker &getGetProcAddressHooker(); |
|
88 |
|
89 static HMODULE WINAPI LoadLibraryA(PCSTR path); |
|
90 |
|
91 private: |
|
92 DHWImportHooker* mNext; |
|
93 const char* mModuleName; |
|
94 const char* mFunctionName; |
|
95 PROC mOriginal; |
|
96 PROC mHook; |
|
97 HMODULE mIgnoreModule; |
|
98 bool mHooking; |
|
99 |
|
100 private: |
|
101 static PRLock* gLock; |
|
102 static DHWImportHooker* gHooks; |
|
103 static decltype(GetProcAddress)* gRealGetProcAddress; |
|
104 |
|
105 static HMODULE WINAPI LoadLibraryW(PCWSTR path); |
|
106 static HMODULE WINAPI LoadLibraryExW(PCWSTR path, HANDLE file, DWORD flags); |
|
107 static HMODULE WINAPI LoadLibraryExA(PCSTR path, HANDLE file, DWORD flags); |
|
108 |
|
109 static FARPROC WINAPI GetProcAddress(HMODULE aModule, PCSTR aFunctionName); |
|
110 }; |
|
111 |
|
112 /***************************************************************************/ |
|
113 // This supports the _CrtSetAllocHook based hooking. |
|
114 // This system sucks because you don't get to see the allocated pointer. I |
|
115 // don't think it appropriate for nsTraceMalloc, but is useful as a means to make |
|
116 // malloc fail for testing purposes. |
|
117 #if 0 //comment out this stuff. not necessary |
|
118 |
|
119 class DHWAllocationSizeDebugHook |
|
120 { |
|
121 public: |
|
122 virtual bool AllocHook(size_t size) = 0; |
|
123 virtual bool ReallocHook(size_t size, size_t sizeOld) = 0; |
|
124 virtual bool FreeHook(size_t size) = 0; |
|
125 }; |
|
126 |
|
127 extern bool dhwSetAllocationSizeDebugHook(DHWAllocationSizeDebugHook* hook); |
|
128 extern bool dhwClearAllocationSizeDebugHook(); |
|
129 |
|
130 /***************************************************************************/ |
|
131 #endif //0 |
|
132 |
|
133 #endif /* __nsDebugHelpWin32_h__ */ |