Bumping manifests a=b2g-bump
[gecko.git] / tools / trace-malloc / lib / nsDebugHelpWin32.h
blobeef1597b2034619792bb18c5f763ed9ae549b671
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/. */
6 /* Win32 x86/x64 code for stack walking, symbol resolution, and function hooking */
8 #ifndef __nsDebugHelpWin32_h__
9 #define __nsDebugHelpWin32_h__
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
22 // XXX temporary hack...
23 //#include "hacky_defines.h"
26 /***************************************************************************/
27 // useful macros...
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
36 /**********************************************************/
37 // This is used to get 'original' function addresses from DHWImportHooker.
39 #define DHW_ORIGINAL(name_, hooker_) \
40 ((decltype(name_)*) hooker_ . GetOriginalFunction())
42 /***************************************************************************/
43 // Global declarations of entry points into ImgHelp functions
45 #ifndef _WIN64
46 DHW_DECLARE_FUN_GLOBAL(EnumerateLoadedModules);
47 #else
48 DHW_DECLARE_FUN_GLOBAL(EnumerateLoadedModules64);
49 #endif
51 DHW_DECLARE_FUN_GLOBAL(ImageDirectoryEntryToData);
53 /***************************************************************************/
55 extern bool
56 dhwEnsureImageHlpInitialized();
58 /***************************************************************************/
60 class DHWImportHooker
62 public:
64 DHWImportHooker(const char* aModuleName,
65 const char* aFunctionName,
66 PROC aHook,
67 bool aExcludeOurModule = false);
69 ~DHWImportHooker();
71 PROC GetOriginalFunction() {return mOriginal;}
73 bool PatchAllModules();
74 bool PatchOneModule(HMODULE aModule, const char* name);
75 static bool ModuleLoaded(HMODULE aModule, DWORD flags);
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.
83 static DHWImportHooker &getLoadLibraryWHooker();
84 static DHWImportHooker &getLoadLibraryExWHooker();
85 static DHWImportHooker &getLoadLibraryAHooker();
86 static DHWImportHooker &getLoadLibraryExAHooker();
87 static DHWImportHooker &getGetProcAddressHooker();
89 static HMODULE WINAPI LoadLibraryA(PCSTR path);
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;
100 private:
101 static PRLock* gLock;
102 static DHWImportHooker* gHooks;
103 static decltype(GetProcAddress)* gRealGetProcAddress;
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);
109 static FARPROC WINAPI GetProcAddress(HMODULE aModule, PCSTR aFunctionName);
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
119 class DHWAllocationSizeDebugHook
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;
127 extern bool dhwSetAllocationSizeDebugHook(DHWAllocationSizeDebugHook* hook);
128 extern bool dhwClearAllocationSizeDebugHook();
130 /***************************************************************************/
131 #endif //0
133 #endif /* __nsDebugHelpWin32_h__ */