OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
11 // copyright notice, this list of conditions and the following disclaimer | 11 // copyright notice, this list of conditions and the following disclaimer |
12 // in the documentation and/or other materials provided with the | 12 // in the documentation and/or other materials provided with the |
13 // distribution. | 13 // distribution. |
14 // * Neither the name of Google Inc. nor the names of its | 14 // * Neither the name of Google Inc. nor the names of its |
15 // contributors may be used to endorse or promote products derived from | 15 // contributors may be used to endorse or promote products derived from |
16 // this software without specific prior written permission. | 16 // this software without specific prior written permission. |
17 // | 17 // |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 /* The code has moved to gperftools/. Use that include-directory for | 30 // --- |
31 * new code. | 31 // Author: Sanjay Ghemawat |
32 */ | 32 // |
33 #include <gperftools/malloc_hook.h> | 33 // Some of our malloc implementations can invoke the following hooks whenever |
| 34 // memory is allocated or deallocated. MallocHook is thread-safe, and things |
| 35 // you do before calling AddFooHook(MyHook) are visible to any resulting calls |
| 36 // to MyHook. Hooks must be thread-safe. If you write: |
| 37 // |
| 38 // CHECK(MallocHook::AddNewHook(&MyNewHook)); |
| 39 // |
| 40 // MyNewHook will be invoked in subsequent calls in the current thread, but |
| 41 // there are no guarantees on when it might be invoked in other threads. |
| 42 // |
| 43 // There are a limited number of slots available for each hook type. Add*Hook |
| 44 // will return false if there are no slots available. Remove*Hook will return |
| 45 // false if the given hook was not already installed. |
| 46 // |
| 47 // The order in which individual hooks are called in Invoke*Hook is undefined. |
| 48 // |
| 49 // It is safe for a hook to remove itself within Invoke*Hook and add other |
| 50 // hooks. Any hooks added inside a hook invocation (for the same hook type) |
| 51 // will not be invoked for the current invocation. |
| 52 // |
| 53 // One important user of these hooks is the heap profiler. |
| 54 // |
| 55 // CAVEAT: If you add new MallocHook::Invoke* calls then those calls must be |
| 56 // directly in the code of the (de)allocation function that is provided to the |
| 57 // user and that function must have an ATTRIBUTE_SECTION(malloc_hook) attribute. |
| 58 // |
| 59 // Note: the Invoke*Hook() functions are defined in malloc_hook-inl.h. If you |
| 60 // need to invoke a hook (which you shouldn't unless you're part of tcmalloc), |
| 61 // be sure to #include malloc_hook-inl.h in addition to malloc_hook.h. |
| 62 // |
| 63 // NOTE FOR C USERS: If you want to use malloc_hook functionality from |
| 64 // a C program, #include malloc_hook_c.h instead of this file. |
| 65 |
| 66 #ifndef _MALLOC_HOOK_H_ |
| 67 #define _MALLOC_HOOK_H_ |
| 68 |
| 69 #include <stddef.h> |
| 70 #include <sys/types.h> |
| 71 extern "C" { |
| 72 #include <google/malloc_hook_c.h> // a C version of the malloc_hook interface |
| 73 } |
| 74 |
| 75 // Annoying stuff for windows -- makes sure clients can import these functions |
| 76 #ifndef PERFTOOLS_DLL_DECL |
| 77 # ifdef _WIN32 |
| 78 # define PERFTOOLS_DLL_DECL __declspec(dllimport) |
| 79 # else |
| 80 # define PERFTOOLS_DLL_DECL |
| 81 # endif |
| 82 #endif |
| 83 |
| 84 // Note: malloc_hook_c.h defines MallocHook_*Hook and |
| 85 // MallocHook_{Add,Remove}*Hook. The version of these inside the MallocHook |
| 86 // class are defined in terms of the malloc_hook_c version. See malloc_hook_c.h |
| 87 // for details of these types/functions. |
| 88 |
| 89 class PERFTOOLS_DLL_DECL MallocHook { |
| 90 public: |
| 91 // The NewHook is invoked whenever an object is allocated. |
| 92 // It may be passed NULL if the allocator returned NULL. |
| 93 typedef MallocHook_NewHook NewHook; |
| 94 inline static bool AddNewHook(NewHook hook) { |
| 95 return MallocHook_AddNewHook(hook); |
| 96 } |
| 97 inline static bool RemoveNewHook(NewHook hook) { |
| 98 return MallocHook_RemoveNewHook(hook); |
| 99 } |
| 100 inline static void InvokeNewHook(const void* p, size_t s); |
| 101 |
| 102 // The DeleteHook is invoked whenever an object is deallocated. |
| 103 // It may be passed NULL if the caller is trying to delete NULL. |
| 104 typedef MallocHook_DeleteHook DeleteHook; |
| 105 inline static bool AddDeleteHook(DeleteHook hook) { |
| 106 return MallocHook_AddDeleteHook(hook); |
| 107 } |
| 108 inline static bool RemoveDeleteHook(DeleteHook hook) { |
| 109 return MallocHook_RemoveDeleteHook(hook); |
| 110 } |
| 111 inline static void InvokeDeleteHook(const void* p); |
| 112 |
| 113 // The PreMmapHook is invoked with mmap or mmap64 arguments just |
| 114 // before the call is actually made. Such a hook may be useful |
| 115 // in memory limited contexts, to catch allocations that will exceed |
| 116 // a memory limit, and take outside actions to increase that limit. |
| 117 typedef MallocHook_PreMmapHook PreMmapHook; |
| 118 inline static bool AddPreMmapHook(PreMmapHook hook) { |
| 119 return MallocHook_AddPreMmapHook(hook); |
| 120 } |
| 121 inline static bool RemovePreMmapHook(PreMmapHook hook) { |
| 122 return MallocHook_RemovePreMmapHook(hook); |
| 123 } |
| 124 inline static void InvokePreMmapHook(const void* start, |
| 125 size_t size, |
| 126 int protection, |
| 127 int flags, |
| 128 int fd, |
| 129 off_t offset); |
| 130 |
| 131 // The MmapReplacement is invoked after the PreMmapHook but before |
| 132 // the call is actually made. The MmapReplacement should return true |
| 133 // if it handled the call, or false if it is still necessary to |
| 134 // call mmap/mmap64. |
| 135 // This should be used only by experts, and users must be be |
| 136 // extremely careful to avoid recursive calls to mmap. The replacement |
| 137 // should be async signal safe. |
| 138 // Only one MmapReplacement is supported. After setting an MmapReplacement |
| 139 // you must call RemoveMmapReplacement before calling SetMmapReplacement |
| 140 // again. |
| 141 typedef MallocHook_MmapReplacement MmapReplacement; |
| 142 inline static bool SetMmapReplacement(MmapReplacement hook) { |
| 143 return MallocHook_SetMmapReplacement(hook); |
| 144 } |
| 145 inline static bool RemoveMmapReplacement(MmapReplacement hook) { |
| 146 return MallocHook_RemoveMmapReplacement(hook); |
| 147 } |
| 148 inline static bool InvokeMmapReplacement(const void* start, |
| 149 size_t size, |
| 150 int protection, |
| 151 int flags, |
| 152 int fd, |
| 153 off_t offset, |
| 154 void** result); |
| 155 |
| 156 |
| 157 // The MmapHook is invoked whenever a region of memory is mapped. |
| 158 // It may be passed MAP_FAILED if the mmap failed. |
| 159 typedef MallocHook_MmapHook MmapHook; |
| 160 inline static bool AddMmapHook(MmapHook hook) { |
| 161 return MallocHook_AddMmapHook(hook); |
| 162 } |
| 163 inline static bool RemoveMmapHook(MmapHook hook) { |
| 164 return MallocHook_RemoveMmapHook(hook); |
| 165 } |
| 166 inline static void InvokeMmapHook(const void* result, |
| 167 const void* start, |
| 168 size_t size, |
| 169 int protection, |
| 170 int flags, |
| 171 int fd, |
| 172 off_t offset); |
| 173 |
| 174 // The MunmapReplacement is invoked with munmap arguments just before |
| 175 // the call is actually made. The MunmapReplacement should return true |
| 176 // if it handled the call, or false if it is still necessary to |
| 177 // call munmap. |
| 178 // This should be used only by experts. The replacement should be |
| 179 // async signal safe. |
| 180 // Only one MunmapReplacement is supported. After setting an |
| 181 // MunmapReplacement you must call RemoveMunmapReplacement before |
| 182 // calling SetMunmapReplacement again. |
| 183 typedef MallocHook_MunmapReplacement MunmapReplacement; |
| 184 inline static bool SetMunmapReplacement(MunmapReplacement hook) { |
| 185 return MallocHook_SetMunmapReplacement(hook); |
| 186 } |
| 187 inline static bool RemoveMunmapReplacement(MunmapReplacement hook) { |
| 188 return MallocHook_RemoveMunmapReplacement(hook); |
| 189 } |
| 190 inline static bool InvokeMunmapReplacement(const void* p, |
| 191 size_t size, |
| 192 int* result); |
| 193 |
| 194 // The MunmapHook is invoked whenever a region of memory is unmapped. |
| 195 typedef MallocHook_MunmapHook MunmapHook; |
| 196 inline static bool AddMunmapHook(MunmapHook hook) { |
| 197 return MallocHook_AddMunmapHook(hook); |
| 198 } |
| 199 inline static bool RemoveMunmapHook(MunmapHook hook) { |
| 200 return MallocHook_RemoveMunmapHook(hook); |
| 201 } |
| 202 inline static void InvokeMunmapHook(const void* p, size_t size); |
| 203 |
| 204 // The MremapHook is invoked whenever a region of memory is remapped. |
| 205 typedef MallocHook_MremapHook MremapHook; |
| 206 inline static bool AddMremapHook(MremapHook hook) { |
| 207 return MallocHook_AddMremapHook(hook); |
| 208 } |
| 209 inline static bool RemoveMremapHook(MremapHook hook) { |
| 210 return MallocHook_RemoveMremapHook(hook); |
| 211 } |
| 212 inline static void InvokeMremapHook(const void* result, |
| 213 const void* old_addr, |
| 214 size_t old_size, |
| 215 size_t new_size, |
| 216 int flags, |
| 217 const void* new_addr); |
| 218 |
| 219 // The PreSbrkHook is invoked just before sbrk is called -- except when |
| 220 // the increment is 0. This is because sbrk(0) is often called |
| 221 // to get the top of the memory stack, and is not actually a |
| 222 // memory-allocation call. It may be useful in memory-limited contexts, |
| 223 // to catch allocations that will exceed the limit and take outside |
| 224 // actions to increase such a limit. |
| 225 typedef MallocHook_PreSbrkHook PreSbrkHook; |
| 226 inline static bool AddPreSbrkHook(PreSbrkHook hook) { |
| 227 return MallocHook_AddPreSbrkHook(hook); |
| 228 } |
| 229 inline static bool RemovePreSbrkHook(PreSbrkHook hook) { |
| 230 return MallocHook_RemovePreSbrkHook(hook); |
| 231 } |
| 232 inline static void InvokePreSbrkHook(std::ptrdiff_t increment); |
| 233 |
| 234 // The SbrkHook is invoked whenever sbrk is called -- except when |
| 235 // the increment is 0. This is because sbrk(0) is often called |
| 236 // to get the top of the memory stack, and is not actually a |
| 237 // memory-allocation call. |
| 238 typedef MallocHook_SbrkHook SbrkHook; |
| 239 inline static bool AddSbrkHook(SbrkHook hook) { |
| 240 return MallocHook_AddSbrkHook(hook); |
| 241 } |
| 242 inline static bool RemoveSbrkHook(SbrkHook hook) { |
| 243 return MallocHook_RemoveSbrkHook(hook); |
| 244 } |
| 245 inline static void InvokeSbrkHook(const void* result, std::ptrdiff_t increment
); |
| 246 |
| 247 // Get the current stack trace. Try to skip all routines up to and |
| 248 // and including the caller of MallocHook::Invoke*. |
| 249 // Use "skip_count" (similarly to GetStackTrace from stacktrace.h) |
| 250 // as a hint about how many routines to skip if better information |
| 251 // is not available. |
| 252 inline static int GetCallerStackTrace(void** result, int max_depth, |
| 253 int skip_count) { |
| 254 return MallocHook_GetCallerStackTrace(result, max_depth, skip_count); |
| 255 } |
| 256 |
| 257 // Unhooked versions of mmap() and munmap(). These should be used |
| 258 // only by experts, since they bypass heapchecking, etc. |
| 259 // Note: These do not run hooks, but they still use the MmapReplacement |
| 260 // and MunmapReplacement. |
| 261 static void* UnhookedMMap(void *start, size_t length, int prot, int flags, |
| 262 int fd, off_t offset); |
| 263 static int UnhookedMUnmap(void *start, size_t length); |
| 264 |
| 265 // The following are DEPRECATED. |
| 266 inline static NewHook GetNewHook(); |
| 267 inline static NewHook SetNewHook(NewHook hook) { |
| 268 return MallocHook_SetNewHook(hook); |
| 269 } |
| 270 |
| 271 inline static DeleteHook GetDeleteHook(); |
| 272 inline static DeleteHook SetDeleteHook(DeleteHook hook) { |
| 273 return MallocHook_SetDeleteHook(hook); |
| 274 } |
| 275 |
| 276 inline static PreMmapHook GetPreMmapHook(); |
| 277 inline static PreMmapHook SetPreMmapHook(PreMmapHook hook) { |
| 278 return MallocHook_SetPreMmapHook(hook); |
| 279 } |
| 280 |
| 281 inline static MmapHook GetMmapHook(); |
| 282 inline static MmapHook SetMmapHook(MmapHook hook) { |
| 283 return MallocHook_SetMmapHook(hook); |
| 284 } |
| 285 |
| 286 inline static MunmapHook GetMunmapHook(); |
| 287 inline static MunmapHook SetMunmapHook(MunmapHook hook) { |
| 288 return MallocHook_SetMunmapHook(hook); |
| 289 } |
| 290 |
| 291 inline static MremapHook GetMremapHook(); |
| 292 inline static MremapHook SetMremapHook(MremapHook hook) { |
| 293 return MallocHook_SetMremapHook(hook); |
| 294 } |
| 295 |
| 296 inline static PreSbrkHook GetPreSbrkHook(); |
| 297 inline static PreSbrkHook SetPreSbrkHook(PreSbrkHook hook) { |
| 298 return MallocHook_SetPreSbrkHook(hook); |
| 299 } |
| 300 |
| 301 inline static SbrkHook GetSbrkHook(); |
| 302 inline static SbrkHook SetSbrkHook(SbrkHook hook) { |
| 303 return MallocHook_SetSbrkHook(hook); |
| 304 } |
| 305 // End of DEPRECATED methods. |
| 306 |
| 307 private: |
| 308 // Slow path versions of Invoke*Hook. |
| 309 static void InvokeNewHookSlow(const void* p, size_t s); |
| 310 static void InvokeDeleteHookSlow(const void* p); |
| 311 static void InvokePreMmapHookSlow(const void* start, |
| 312 size_t size, |
| 313 int protection, |
| 314 int flags, |
| 315 int fd, |
| 316 off_t offset); |
| 317 static void InvokeMmapHookSlow(const void* result, |
| 318 const void* start, |
| 319 size_t size, |
| 320 int protection, |
| 321 int flags, |
| 322 int fd, |
| 323 off_t offset); |
| 324 static bool InvokeMmapReplacementSlow(const void* start, |
| 325 size_t size, |
| 326 int protection, |
| 327 int flags, |
| 328 int fd, |
| 329 off_t offset, |
| 330 void** result); |
| 331 static void InvokeMunmapHookSlow(const void* p, size_t size); |
| 332 static bool InvokeMunmapReplacementSlow(const void* p, |
| 333 size_t size, |
| 334 int* result); |
| 335 static void InvokeMremapHookSlow(const void* result, |
| 336 const void* old_addr, |
| 337 size_t old_size, |
| 338 size_t new_size, |
| 339 int flags, |
| 340 const void* new_addr); |
| 341 static void InvokePreSbrkHookSlow(std::ptrdiff_t increment); |
| 342 static void InvokeSbrkHookSlow(const void* result, std::ptrdiff_t increment); |
| 343 }; |
| 344 |
| 345 #endif /* _MALLOC_HOOK_H_ */ |
OLD | NEW |