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