|
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include <stdio.h> |
|
8 #include <stdlib.h> |
|
9 |
|
10 #include "prinrval.h" |
|
11 #include "prlock.h" |
|
12 #include "nscore.h" |
|
13 |
|
14 #include "nsDebugHelpWin32.h" |
|
15 #include "nsTraceMallocCallbacks.h" |
|
16 |
|
17 /***************************************************************************/ |
|
18 // shows how to use the dhw stuff to hook imported functions |
|
19 |
|
20 #if _MSC_VER < 1300 |
|
21 #define NS_DEBUG_CRT "MSVCRTD.dll" |
|
22 #elif _MSC_VER == 1300 |
|
23 #define NS_DEBUG_CRT "msvcr70d.dll" |
|
24 #elif _MSC_VER == 1310 |
|
25 #define NS_DEBUG_CRT "msvcr71d.dll" |
|
26 #elif _MSC_VER == 1400 |
|
27 #define NS_DEBUG_CRT "msvcr80d.dll" |
|
28 #elif _MSC_VER == 1500 |
|
29 #define NS_DEBUG_CRT "msvcr90d.dll" |
|
30 #elif _MSC_VER == 1600 |
|
31 #define NS_DEBUG_CRT "msvcr100d.dll" |
|
32 #elif _MSC_VER == 1700 |
|
33 #define NS_DEBUG_CRT "msvcr110d.dll" |
|
34 #elif _MSC_VER == 1800 |
|
35 #define NS_DEBUG_CRT "msvcr120d.dll" |
|
36 #else |
|
37 #error "Don't know filename of MSVC debug library." |
|
38 #endif |
|
39 |
|
40 decltype(malloc) dhw_malloc; |
|
41 |
|
42 DHWImportHooker &getMallocHooker() |
|
43 { |
|
44 static DHWImportHooker gMallocHooker(NS_DEBUG_CRT, "malloc", (PROC) dhw_malloc); |
|
45 return gMallocHooker; |
|
46 } |
|
47 |
|
48 void * __cdecl dhw_malloc( size_t size ) |
|
49 { |
|
50 tm_thread *t = tm_get_thread(); |
|
51 ++t->suppress_tracing; |
|
52 uint32_t start = PR_IntervalNow(); |
|
53 void* result = DHW_ORIGINAL(malloc, getMallocHooker())(size); |
|
54 uint32_t end = PR_IntervalNow(); |
|
55 --t->suppress_tracing; |
|
56 MallocCallback(result, size, start, end, t); |
|
57 return result; |
|
58 } |
|
59 |
|
60 decltype(calloc) dhw_calloc; |
|
61 |
|
62 DHWImportHooker &getCallocHooker() |
|
63 { |
|
64 static DHWImportHooker gCallocHooker(NS_DEBUG_CRT, "calloc", (PROC) dhw_calloc); |
|
65 return gCallocHooker; |
|
66 } |
|
67 |
|
68 void * __cdecl dhw_calloc( size_t count, size_t size ) |
|
69 { |
|
70 tm_thread *t = tm_get_thread(); |
|
71 ++t->suppress_tracing; |
|
72 uint32_t start = PR_IntervalNow(); |
|
73 void* result = DHW_ORIGINAL(calloc, getCallocHooker())(count,size); |
|
74 uint32_t end = PR_IntervalNow(); |
|
75 --t->suppress_tracing; |
|
76 CallocCallback(result, count, size, start, end, t); |
|
77 return result; |
|
78 } |
|
79 |
|
80 decltype(free) dhw_free; |
|
81 DHWImportHooker &getFreeHooker() |
|
82 { |
|
83 static DHWImportHooker gFreeHooker(NS_DEBUG_CRT, "free", (PROC) dhw_free); |
|
84 return gFreeHooker; |
|
85 } |
|
86 |
|
87 void __cdecl dhw_free( void* p ) |
|
88 { |
|
89 tm_thread *t = tm_get_thread(); |
|
90 ++t->suppress_tracing; |
|
91 uint32_t start = PR_IntervalNow(); |
|
92 DHW_ORIGINAL(free, getFreeHooker())(p); |
|
93 uint32_t end = PR_IntervalNow(); |
|
94 --t->suppress_tracing; |
|
95 /* FIXME bug 392008: We could race with reallocation of p. */ |
|
96 FreeCallback(p, start, end, t); |
|
97 } |
|
98 |
|
99 |
|
100 decltype(realloc) dhw_realloc; |
|
101 DHWImportHooker &getReallocHooker() |
|
102 { |
|
103 static DHWImportHooker gReallocHooker(NS_DEBUG_CRT, "realloc", (PROC) dhw_realloc); |
|
104 return gReallocHooker; |
|
105 } |
|
106 |
|
107 void * __cdecl dhw_realloc(void * pin, size_t size) |
|
108 { |
|
109 tm_thread *t = tm_get_thread(); |
|
110 ++t->suppress_tracing; |
|
111 uint32_t start = PR_IntervalNow(); |
|
112 void* pout = DHW_ORIGINAL(realloc, getReallocHooker())(pin, size); |
|
113 uint32_t end = PR_IntervalNow(); |
|
114 --t->suppress_tracing; |
|
115 /* FIXME bug 392008: We could race with reallocation of pin. */ |
|
116 ReallocCallback(pin, pout, size, start, end, t); |
|
117 return pout; |
|
118 } |
|
119 |
|
120 // Note the mangled name! |
|
121 void * __cdecl dhw_new(size_t size); |
|
122 DHWImportHooker &getNewHooker() |
|
123 { |
|
124 static DHWImportHooker gNewHooker(NS_DEBUG_CRT, "??2@YAPAXI@Z", (PROC) dhw_new); |
|
125 return gNewHooker; |
|
126 } |
|
127 |
|
128 void * __cdecl dhw_new(size_t size) |
|
129 { |
|
130 tm_thread *t = tm_get_thread(); |
|
131 ++t->suppress_tracing; |
|
132 uint32_t start = PR_IntervalNow(); |
|
133 void* result = DHW_ORIGINAL(dhw_new, getNewHooker())(size); |
|
134 uint32_t end = PR_IntervalNow(); |
|
135 --t->suppress_tracing; |
|
136 MallocCallback(result, size, start, end, t);//do we need a different one for new? |
|
137 return result; |
|
138 } |
|
139 |
|
140 // Note the mangled name! |
|
141 void __cdecl dhw_delete(void* p); |
|
142 DHWImportHooker &getDeleteHooker() |
|
143 { |
|
144 static DHWImportHooker gDeleteHooker(NS_DEBUG_CRT, "??3@YAXPAX@Z", (PROC) dhw_delete); |
|
145 return gDeleteHooker; |
|
146 } |
|
147 |
|
148 void __cdecl dhw_delete(void* p) |
|
149 { |
|
150 tm_thread *t = tm_get_thread(); |
|
151 ++t->suppress_tracing; |
|
152 uint32_t start = PR_IntervalNow(); |
|
153 DHW_ORIGINAL(dhw_delete, getDeleteHooker())(p); |
|
154 uint32_t end = PR_IntervalNow(); |
|
155 --t->suppress_tracing; |
|
156 FreeCallback(p, start, end, t); |
|
157 } |
|
158 |
|
159 // Note the mangled name! |
|
160 void * __cdecl dhw_vec_new(size_t size); |
|
161 DHWImportHooker &getVecNewHooker() |
|
162 { |
|
163 static DHWImportHooker gVecNewHooker(NS_DEBUG_CRT, "??_U@YAPAXI@Z", (PROC) dhw_vec_new); |
|
164 return gVecNewHooker; |
|
165 } |
|
166 |
|
167 void * __cdecl dhw_vec_new(size_t size) |
|
168 { |
|
169 tm_thread *t = tm_get_thread(); |
|
170 ++t->suppress_tracing; // need to suppress since new[] calls new |
|
171 uint32_t start = PR_IntervalNow(); |
|
172 void* result = DHW_ORIGINAL(dhw_vec_new, getVecNewHooker())(size); |
|
173 uint32_t end = PR_IntervalNow(); |
|
174 --t->suppress_tracing; |
|
175 MallocCallback(result, size, start, end, t);//do we need a different one for new[]? |
|
176 return result; |
|
177 } |
|
178 |
|
179 // Note the mangled name! |
|
180 void __cdecl dhw_vec_delete(void* p); |
|
181 DHWImportHooker &getVecDeleteHooker() |
|
182 { |
|
183 static DHWImportHooker gVecDeleteHooker(NS_DEBUG_CRT, "??_V@YAXPAX@Z", (PROC) dhw_vec_delete); |
|
184 return gVecDeleteHooker; |
|
185 } |
|
186 |
|
187 void __cdecl dhw_vec_delete(void* p) |
|
188 { |
|
189 tm_thread *t = tm_get_thread(); |
|
190 ++t->suppress_tracing; |
|
191 uint32_t start = PR_IntervalNow(); |
|
192 DHW_ORIGINAL(dhw_vec_delete, getVecDeleteHooker())(p); |
|
193 uint32_t end = PR_IntervalNow(); |
|
194 --t->suppress_tracing; |
|
195 FreeCallback(p, start, end, t); |
|
196 } |
|
197 |
|
198 /*C Callbacks*/ |
|
199 PR_IMPLEMENT(void) |
|
200 StartupHooker() |
|
201 { |
|
202 //run through get all hookers |
|
203 DHWImportHooker &loadlibraryW = DHWImportHooker::getLoadLibraryWHooker(); |
|
204 DHWImportHooker &loadlibraryExW = DHWImportHooker::getLoadLibraryExWHooker(); |
|
205 DHWImportHooker &loadlibraryA = DHWImportHooker::getLoadLibraryAHooker(); |
|
206 DHWImportHooker &loadlibraryExA = DHWImportHooker::getLoadLibraryExAHooker(); |
|
207 DHWImportHooker &mallochooker = getMallocHooker(); |
|
208 DHWImportHooker &reallochooker = getReallocHooker(); |
|
209 DHWImportHooker &callochooker = getCallocHooker(); |
|
210 DHWImportHooker &freehooker = getFreeHooker(); |
|
211 DHWImportHooker &newhooker = getNewHooker(); |
|
212 DHWImportHooker &deletehooker = getDeleteHooker(); |
|
213 DHWImportHooker &vecnewhooker = getVecNewHooker(); |
|
214 DHWImportHooker &vecdeletehooker = getVecDeleteHooker(); |
|
215 printf("Startup Hooker\n"); |
|
216 } |
|
217 |
|
218 PR_IMPLEMENT(void) |
|
219 ShutdownHooker() |
|
220 { |
|
221 } |
|
222 |
|
223 extern "C" { |
|
224 void* dhw_orig_malloc(size_t); |
|
225 void* dhw_orig_calloc(size_t, size_t); |
|
226 void* dhw_orig_realloc(void*, size_t); |
|
227 void dhw_orig_free(void*); |
|
228 } |
|
229 |
|
230 void* |
|
231 dhw_orig_malloc(size_t size) |
|
232 { |
|
233 return DHW_ORIGINAL(malloc, getMallocHooker())(size); |
|
234 } |
|
235 |
|
236 void* |
|
237 dhw_orig_calloc(size_t count, size_t size) |
|
238 { |
|
239 return DHW_ORIGINAL(calloc, getCallocHooker())(count,size); |
|
240 } |
|
241 |
|
242 void* |
|
243 dhw_orig_realloc(void* pin, size_t size) |
|
244 { |
|
245 return DHW_ORIGINAL(realloc, getReallocHooker())(pin, size); |
|
246 } |
|
247 |
|
248 void |
|
249 dhw_orig_free(void* p) |
|
250 { |
|
251 DHW_ORIGINAL(free, getFreeHooker())(p); |
|
252 } |