1 ////////////////////////////////////////////////////////////////////////////////
3 // Visual Leak Detector - Various Utility Definitions
4 // Copyright (c) 2005-2006 Dan Moulding
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 // See COPYING.txt for the full terms of the GNU Lesser General Public License.
22 ////////////////////////////////////////////////////////////////////////////////
28 "This header should only be included by Visual Leak Detector when building it from source. \
29 Applications should never include this header."
36 #define ADDRESSFORMAT L"0x%.16X" // Format string for 64-bit addresses
38 #define ADDRESSFORMAT L"0x%.8X" // Format string for 32-bit addresses
40 #define BOM 0xFEFF // Unicode byte-order mark.
41 #define MAXREPORTLENGTH 511 // Maximum length, in characters, of "report" messages.
43 // Architecture-specific definitions for x86 and x64
46 #define X86X64ARCHITECTURE IMAGE_FILE_MACHINE_I386
53 #define X86X64ARCHITECTURE IMAGE_FILE_MACHINE_AMD64
60 #if defined(_M_IX86) || defined (_M_X64)
61 #define FRAMEPOINTER(fp) __asm {mov fp, BPREG} // Copies the current frame pointer to the supplied variable.
63 // If you want to retarget Visual Leak Detector to another processor
64 // architecture then you'll need to provide an architecture-specific macro to
65 // obtain the frame pointer (or other address) which can be used to obtain the
66 // return address and stack pointer of the calling frame.
67 #error "Visual Leak Detector is not supported on this architecture."
68 #endif // _M_IX86 || _M_X64
70 // Miscellaneous definitions
71 #define R2VA(modulebase, rva) (((PBYTE)modulebase) + rva) // Relative Virtual Address to Virtual Address conversion.
72 #define BYTEFORMATBUFFERLENGTH 4
73 #define HEXDUMPLINELENGTH 58
75 // Reports can be encoded as either ASCII or Unicode (UTF-16).
81 // This structure allows us to build a table of APIs which should be patched
82 // through to replacement functions provided by VLD.
83 typedef struct patchentry_s
85 LPCSTR exportmodulename; // The name of the module exporting the patched API.
86 LPCSTR importname; // The name (or ordinal) of the imported API being patched.
87 SIZE_T modulebase; // The base address of the exporting module (filled in at runtime when the modules are loaded).
88 LPCVOID replacement; // Pointer to the function to which the imported API should be patched through to.
91 // Utility functions. See function definitions for details.
92 VOID dumpmemorya (LPCVOID address, SIZE_T length);
93 VOID dumpmemoryw (LPCVOID address, SIZE_T length);
94 BOOL findimport (HMODULE importmodule, HMODULE exportmodule, LPCSTR exportmodulename, LPCSTR importname);
95 BOOL findpatch (HMODULE importmodule, LPCSTR exportmodulename, LPCVOID replacement);
96 VOID insertreportdelay ();
97 BOOL moduleispatched (HMODULE importmodule, patchentry_t patchtable [], UINT tablesize);
98 BOOL patchimport (HMODULE importmodule, HMODULE exportmodule, LPCSTR exportmodulename, LPCSTR importname,
100 BOOL patchmodule (HMODULE importmodule, patchentry_t patchtable [], UINT tablesize);
101 VOID report (LPCWSTR format, ...);
102 VOID restoreimport (HMODULE importmodule, HMODULE exportmodule, LPCSTR exportmodulename, LPCSTR importname,
103 LPCVOID replacement);
104 VOID restoremodule (HMODULE importmodule, patchentry_t patchtable [], UINT tablesize);
105 VOID setreportencoding (encoding_e encoding);
106 VOID setreportfile (FILE *file, BOOL copydebugger);
107 VOID strapp (LPWSTR *dest, LPCWSTR source);
108 BOOL strtobool (LPCWSTR s);
109 #if _WIN32_WINNT < 0x0600 // Windows XP or earlier, no GetProcessIdOfThread()
110 DWORD _GetProcessIdOfThread (HANDLE thread);
111 #define GetProcessIdOfThread _GetProcessIdOfThread