MCPcopy Create free account
hub / github.com/async-profiler/async-profiler / patchLibraries

Method patchLibraries

src/mallocTracer.cpp:183–212  ·  view source on GitHub ↗

To avoid complexity in hooking and tracking reentrancy, a TLS-based approach is not used. Reentrant allocation calls would result in double-accounting. However, this does not impact the leak detector, as it correctly tracks memory as freed regardless of how many times recordMalloc is called with the same address.

Source from the content-addressed store, hash-verified

181// the leak detector, as it correctly tracks memory as freed regardless of how many times
182// recordMalloc is called with the same address.
183void MallocTracer::patchLibraries() {
184 MutexLocker ml(_patch_lock);
185
186 CodeCacheArray* native_libs = Profiler::instance()->nativeLibs();
187 int native_lib_count = native_libs->count();
188
189 while (_patched_libs < native_lib_count) {
190 CodeCache* cc = (*native_libs)[_patched_libs++];
191
192 UnloadProtection handle(cc);
193 if (!handle.isValid()) {
194 continue;
195 }
196
197 cc->patchImport(im_malloc, (void*)malloc_hook);
198 cc->patchImport(im_realloc, (void*)realloc_hook);
199 cc->patchImport(im_free, (void*)free_hook);
200 cc->patchImport(im_aligned_alloc, (void*)aligned_alloc_hook);
201
202 if (_nested_malloc) {
203 // Use dummy hooks to prevent double-accounting. Dummy frames from AP are introduced
204 // to preserve the frame link to the original caller (see #1226).
205 cc->patchImport(im_calloc, (void*)calloc_hook_dummy);
206 cc->patchImport(im_posix_memalign, (void*)posix_memalign_hook_dummy);
207 } else {
208 cc->patchImport(im_calloc, (void*)calloc_hook);
209 cc->patchImport(im_posix_memalign, (void*)posix_memalign_hook);
210 }
211 }
212}
213
214void MallocTracer::recordMalloc(void* address, size_t size) {
215 if (updateCounter(_allocated_bytes, size, _interval)) {

Callers

nothing calls this directly

Calls 4

nativeLibsMethod · 0.80
isValidMethod · 0.80
patchImportMethod · 0.80
countMethod · 0.45

Tested by

no test coverage detected