]> git.lyx.org Git - lyx.git/blob - development/Win32/vld/src/utility.h
installer: updated for dictionaries
[lyx.git] / development / Win32 / vld / src / utility.h
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 //  Visual Leak Detector - Various Utility Definitions
4 //  Copyright (c) 2005-2006 Dan Moulding
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See COPYING.txt for the full terms of the GNU Lesser General Public License.
21 //
22 ////////////////////////////////////////////////////////////////////////////////
23
24 #pragma once
25
26 #ifndef VLDBUILD
27 #error \
28 "This header should only be included by Visual Leak Detector when building it from source. \
29 Applications should never include this header."
30 #endif
31
32 #include <cstdio>
33 #include <windows.h>
34
35 #ifdef _WIN64
36 #define ADDRESSFORMAT   L"0x%.16X" // Format string for 64-bit addresses
37 #else
38 #define ADDRESSFORMAT   L"0x%.8X"  // Format string for 32-bit addresses
39 #endif // _WIN64
40 #define BOM             0xFEFF     // Unicode byte-order mark.
41 #define MAXREPORTLENGTH 511        // Maximum length, in characters, of "report" messages.
42
43 // Architecture-specific definitions for x86 and x64
44 #if defined(_M_IX86)
45 #define SIZEOFPTR 4
46 #define X86X64ARCHITECTURE IMAGE_FILE_MACHINE_I386
47 #define AXREG Eax
48 #define BPREG Ebp
49 #define IPREG Eip
50 #define SPREG Esp
51 #elif defined(_M_X64)
52 #define SIZEOFPTR 8
53 #define X86X64ARCHITECTURE IMAGE_FILE_MACHINE_AMD64
54 #define AXREG Rax
55 #define BPREG Rbp
56 #define IPREG Rip
57 #define SPREG Rsp
58 #endif // _M_IX86
59
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.
62 #else
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
69
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
74
75 // Reports can be encoded as either ASCII or Unicode (UTF-16).
76 enum encoding_e {
77     ascii,
78     unicode
79 };
80
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
84 {
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.
89 } patchentry_t;
90
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,
99                   LPCVOID replacement);
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
112 #endif