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