blob: 114579626cf232d4af045bad32fe156953ee98ae [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <errno.h>
30#include <inttypes.h>
31#include <malloc.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080032#include <stdio.h>
Christopher Ferrisc328e442019-04-01 19:31:26 -070033#include <stdlib.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080034#include <string.h>
35#include <sys/cdefs.h>
36#include <sys/param.h>
37#include <unistd.h>
38
Christopher Ferris602b88c2017-08-04 13:04:04 -070039#include <mutex>
Christopher Ferris63860cb2015-11-16 17:30:32 -080040#include <vector>
41
Christopher Ferris602b88c2017-08-04 13:04:04 -070042#include <android-base/file.h>
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070043#include <android-base/properties.h>
Christopher Ferris602b88c2017-08-04 13:04:04 -070044#include <android-base/stringprintf.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080045#include <private/bionic_malloc_dispatch.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080046#include <private/MallocXmlElem.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080047
Christopher Ferris72df6702016-02-11 15:51:31 -080048#include "Config.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080049#include "DebugData.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080050#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080051#include "debug_disable.h"
52#include "debug_log.h"
53#include "malloc_debug.h"
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070054#include "UnwindBacktrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080055
56// ------------------------------------------------------------------------
57// Global Data
58// ------------------------------------------------------------------------
59DebugData* g_debug;
60
61int* g_malloc_zygote_child;
62
63const MallocDispatch* g_dispatch;
64// ------------------------------------------------------------------------
65
66// ------------------------------------------------------------------------
67// Use C style prototypes for all exported functions. This makes it easy
68// to do dlsym lookups during libc initialization when malloc debug
69// is enabled.
70// ------------------------------------------------------------------------
71__BEGIN_DECLS
72
Tamas Berghammerac81fe82016-08-26 15:54:59 +010073bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -080074 const char* options);
Christopher Ferris63860cb2015-11-16 17:30:32 -080075void debug_finalize();
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070076void debug_dump_heap(const char* file_name);
Christopher Ferris4da25032018-03-07 13:38:48 -080077void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
78 size_t* total_memory, size_t* backtrace_size);
Christopher Ferris2e1a40a2018-06-13 10:46:34 -070079bool debug_write_malloc_leak_info(FILE* fp);
Colin Cross2d4721c2016-02-02 11:57:54 -080080ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count);
Christopher Ferris63860cb2015-11-16 17:30:32 -080081void debug_free_malloc_leak_info(uint8_t* info);
82size_t debug_malloc_usable_size(void* pointer);
83void* debug_malloc(size_t size);
84void debug_free(void* pointer);
Christopher Ferriscae21a92018-02-05 18:14:55 -080085void* debug_aligned_alloc(size_t alignment, size_t size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080086void* debug_memalign(size_t alignment, size_t bytes);
87void* debug_realloc(void* pointer, size_t bytes);
88void* debug_calloc(size_t nmemb, size_t bytes);
89struct mallinfo debug_mallinfo();
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070090int debug_mallopt(int param, int value);
Christopher Ferris6c619a02019-03-01 17:59:51 -080091int debug_malloc_info(int options, FILE* fp);
Christopher Ferris63860cb2015-11-16 17:30:32 -080092int debug_posix_memalign(void** memptr, size_t alignment, size_t size);
Colin Cross869691c2016-01-29 12:48:18 -080093int debug_iterate(uintptr_t base, size_t size,
Christopher Ferris4da25032018-03-07 13:38:48 -080094 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg);
Colin Cross869691c2016-01-29 12:48:18 -080095void debug_malloc_disable();
96void debug_malloc_enable();
Christopher Ferris63860cb2015-11-16 17:30:32 -080097
98#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
99void* debug_pvalloc(size_t bytes);
100void* debug_valloc(size_t size);
101#endif
102
103__END_DECLS
104// ------------------------------------------------------------------------
105
Colin Cross7a28a3c2016-02-07 22:51:15 -0800106static void InitAtfork() {
107 static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
Christopher Ferris4da25032018-03-07 13:38:48 -0800108 pthread_once(&atfork_init, []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800109 pthread_atfork(
Christopher Ferris4da25032018-03-07 13:38:48 -0800110 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800111 if (g_debug != nullptr) {
112 g_debug->PrepareFork();
113 }
114 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800115 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800116 if (g_debug != nullptr) {
117 g_debug->PostForkParent();
118 }
119 },
Christopher Ferris4da25032018-03-07 13:38:48 -0800120 []() {
Colin Cross7a28a3c2016-02-07 22:51:15 -0800121 if (g_debug != nullptr) {
122 g_debug->PostForkChild();
123 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800124 });
Colin Cross7a28a3c2016-02-07 22:51:15 -0800125 });
126}
Christopher Ferrisd0919622016-03-15 22:39:39 -0700127
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700128void BacktraceAndLog() {
129 if (g_debug->config().options() & BACKTRACE_FULL) {
130 std::vector<uintptr_t> frames;
131 std::vector<unwindstack::LocalFrameData> frames_info;
132 if (!Unwind(&frames, &frames_info, 256)) {
133 error_log(" Backtrace failed to get any frames.");
134 } else {
135 UnwindLog(frames_info);
136 }
137 } else {
138 std::vector<uintptr_t> frames(256);
139 size_t num_frames = backtrace_get(frames.data(), frames.size());
140 if (num_frames == 0) {
141 error_log(" Backtrace failed to get any frames.");
142 } else {
143 backtrace_log(frames.data(), num_frames);
144 }
145 }
146}
147
Christopher Ferris4da25032018-03-07 13:38:48 -0800148static void LogError(const void* pointer, const char* error_str) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800149 error_log(LOG_DIVIDER);
Christopher Ferris4da25032018-03-07 13:38:48 -0800150 error_log("+++ ALLOCATION %p %s", pointer, error_str);
151
152 // If we are tracking already freed pointers, check to see if this is
153 // one so we can print extra information.
154 if (g_debug->config().options() & FREE_TRACK) {
155 PointerData::LogFreeBacktrace(pointer);
Christopher Ferris7993b802016-01-28 18:35:05 -0800156 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800157
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700158 error_log("Backtrace at time of failure:");
159 BacktraceAndLog();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800160 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800161 if (g_debug->config().options() & ABORT_ON_ERROR) {
162 abort();
163 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800164}
165
Christopher Ferris4da25032018-03-07 13:38:48 -0800166static bool VerifyPointer(const void* pointer, const char* function_name) {
167 if (g_debug->HeaderEnabled()) {
168 Header* header = g_debug->GetHeader(pointer);
169 if (header->tag != DEBUG_TAG) {
170 std::string error_str;
171 if (header->tag == DEBUG_FREE_TAG) {
172 error_str = std::string("USED AFTER FREE (") + function_name + ")";
173 } else {
174 error_str = android::base::StringPrintf("HAS INVALID TAG %" PRIx32 " (%s)", header->tag,
175 function_name);
176 }
177 LogError(pointer, error_str.c_str());
178 return false;
179 }
180 }
181
182 if (g_debug->TrackPointers()) {
183 if (!PointerData::Exists(pointer)) {
184 std::string error_str(std::string("UNKNOWN POINTER (") + function_name + ")");
185 LogError(pointer, error_str.c_str());
186 return false;
187 }
188 }
189 return true;
190}
191
192static size_t InternalMallocUsableSize(void* pointer) {
193 if (g_debug->HeaderEnabled()) {
194 return g_debug->GetHeader(pointer)->usable_size;
195 } else {
196 return g_dispatch->malloc_usable_size(pointer);
197 }
198}
199
Christopher Ferris63860cb2015-11-16 17:30:32 -0800200static void* InitHeader(Header* header, void* orig_pointer, size_t size) {
201 header->tag = DEBUG_TAG;
202 header->orig_pointer = orig_pointer;
203 header->size = size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800204 header->usable_size = g_dispatch->malloc_usable_size(orig_pointer);
205 if (header->usable_size == 0) {
206 g_dispatch->free(orig_pointer);
207 return nullptr;
208 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800209 header->usable_size -= g_debug->pointer_offset() + reinterpret_cast<uintptr_t>(header) -
210 reinterpret_cast<uintptr_t>(orig_pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800211
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700212 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800213 uint8_t* guard = g_debug->GetFrontGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700214 memset(guard, g_debug->config().front_guard_value(), g_debug->config().front_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800215 }
216
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700217 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800218 uint8_t* guard = g_debug->GetRearGuard(header);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700219 memset(guard, g_debug->config().rear_guard_value(), g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800220 // If the rear guard is enabled, set the usable size to the exact size
221 // of the allocation.
Christopher Ferris4da25032018-03-07 13:38:48 -0800222 header->usable_size = header->size;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800223 }
224
225 return g_debug->GetPointer(header);
226}
227
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100228bool debug_initialize(const MallocDispatch* malloc_dispatch, int* malloc_zygote_child,
Christopher Ferris4da25032018-03-07 13:38:48 -0800229 const char* options) {
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100230 if (malloc_zygote_child == nullptr || options == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800231 return false;
232 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800233
234 InitAtfork();
235
Christopher Ferris63860cb2015-11-16 17:30:32 -0800236 g_malloc_zygote_child = malloc_zygote_child;
237
238 g_dispatch = malloc_dispatch;
239
240 if (!DebugDisableInitialize()) {
241 return false;
242 }
243
244 DebugData* debug = new DebugData();
Tamas Berghammerac81fe82016-08-26 15:54:59 +0100245 if (!debug->Initialize(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800246 delete debug;
247 DebugDisableFinalize();
248 return false;
249 }
250 g_debug = debug;
251
252 // Always enable the backtrace code since we will use it in a number
253 // of different error cases.
254 backtrace_startup();
255
Christopher Ferrisc328e442019-04-01 19:31:26 -0700256 if (g_debug->config().options() & VERBOSE) {
257 info_log("%s: malloc debug enabled", getprogname());
258 }
259
Christopher Ferris63860cb2015-11-16 17:30:32 -0800260 return true;
261}
262
263void debug_finalize() {
264 if (g_debug == nullptr) {
265 return;
266 }
267
Christopher Ferris97b47472018-07-10 14:45:24 -0700268 // Turn off capturing allocations calls.
269 DebugDisableSet(true);
270
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700271 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800272 PointerData::VerifyAllFreed();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800273 }
274
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700275 if (g_debug->config().options() & LEAK_TRACK) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800276 PointerData::LogLeaks();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800277 }
278
Christopher Ferris602b88c2017-08-04 13:04:04 -0700279 if ((g_debug->config().options() & BACKTRACE) && g_debug->config().backtrace_dump_on_exit()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800280 debug_dump_heap(android::base::StringPrintf("%s.%d.exit.txt",
281 g_debug->config().backtrace_dump_prefix().c_str(),
Christopher Ferris97b47472018-07-10 14:45:24 -0700282 getpid()).c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700283 }
284
Colin Cross2c759912016-02-05 16:17:39 -0800285 backtrace_shutdown();
286
Christopher Ferris63860cb2015-11-16 17:30:32 -0800287 delete g_debug;
288 g_debug = nullptr;
289
290 DebugDisableFinalize();
291}
292
Christopher Ferris4da25032018-03-07 13:38:48 -0800293void debug_get_malloc_leak_info(uint8_t** info, size_t* overall_size, size_t* info_size,
294 size_t* total_memory, size_t* backtrace_size) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800295 ScopedDisableDebugCalls disable;
296
297 // Verify the arguments.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700298 if (info == nullptr || overall_size == nullptr || info_size == nullptr || total_memory == nullptr ||
Christopher Ferris4da25032018-03-07 13:38:48 -0800299 backtrace_size == nullptr) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800300 error_log("get_malloc_leak_info: At least one invalid parameter.");
301 return;
302 }
303
304 *info = nullptr;
305 *overall_size = 0;
306 *info_size = 0;
307 *total_memory = 0;
308 *backtrace_size = 0;
309
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700310 if (!(g_debug->config().options() & BACKTRACE)) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800311 error_log(
312 "get_malloc_leak_info: Allocations not being tracked, to enable "
313 "set the option 'backtrace'.");
Christopher Ferris63860cb2015-11-16 17:30:32 -0800314 return;
315 }
316
Christopher Ferris4da25032018-03-07 13:38:48 -0800317 PointerData::GetInfo(info, overall_size, info_size, total_memory, backtrace_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800318}
319
320void debug_free_malloc_leak_info(uint8_t* info) {
321 g_dispatch->free(info);
322}
323
Christopher Ferris55a89a42016-04-07 17:14:53 -0700324size_t debug_malloc_usable_size(void* pointer) {
325 if (DebugCallsDisabled() || pointer == nullptr) {
326 return g_dispatch->malloc_usable_size(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800327 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700328 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800329
Christopher Ferris4da25032018-03-07 13:38:48 -0800330 if (!VerifyPointer(pointer, "malloc_usable_size")) {
331 return 0;
332 }
333
334 return InternalMallocUsableSize(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700335}
336
Christopher Ferris4da25032018-03-07 13:38:48 -0800337static void* InternalMalloc(size_t size) {
338 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
339 debug_dump_heap(android::base::StringPrintf(
340 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
341 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700342 }
343
Colin Cross9567c7b2016-03-09 17:56:14 -0800344 if (size == 0) {
345 size = 1;
346 }
347
Christopher Ferris63860cb2015-11-16 17:30:32 -0800348 size_t real_size = size + g_debug->extra_bytes();
349 if (real_size < size) {
350 // Overflow.
351 errno = ENOMEM;
352 return nullptr;
353 }
354
Christopher Ferris4da25032018-03-07 13:38:48 -0800355 if (size > PointerInfoType::MaxSize()) {
356 errno = ENOMEM;
357 return nullptr;
358 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800359
Christopher Ferris4da25032018-03-07 13:38:48 -0800360 void* pointer;
361 if (g_debug->HeaderEnabled()) {
362 Header* header =
363 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800364 if (header == nullptr) {
365 return nullptr;
366 }
367 pointer = InitHeader(header, header, size);
368 } else {
369 pointer = g_dispatch->malloc(real_size);
370 }
371
Christopher Ferris4da25032018-03-07 13:38:48 -0800372 if (pointer != nullptr) {
373 if (g_debug->TrackPointers()) {
374 PointerData::Add(pointer, size);
375 }
376
377 if (g_debug->config().options() & FILL_ON_ALLOC) {
378 size_t bytes = InternalMallocUsableSize(pointer);
379 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
380 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
381 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
382 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800383 }
384 return pointer;
385}
386
Christopher Ferris55a89a42016-04-07 17:14:53 -0700387void* debug_malloc(size_t size) {
388 if (DebugCallsDisabled()) {
389 return g_dispatch->malloc(size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800390 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700391 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800392
Christopher Ferris4da25032018-03-07 13:38:48 -0800393 void* pointer = InternalMalloc(size);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700394
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700395 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700396 g_debug->record->AddEntry(new MallocEntry(pointer, size));
397 }
398
399 return pointer;
Christopher Ferris55a89a42016-04-07 17:14:53 -0700400}
401
Christopher Ferris4da25032018-03-07 13:38:48 -0800402static void InternalFree(void* pointer) {
403 if ((g_debug->config().options() & BACKTRACE) && g_debug->pointer->ShouldDumpAndReset()) {
404 debug_dump_heap(android::base::StringPrintf(
405 "%s.%d.txt", g_debug->config().backtrace_dump_prefix().c_str(), getpid())
406 .c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700407 }
408
Christopher Ferris63860cb2015-11-16 17:30:32 -0800409 void* free_pointer = pointer;
410 size_t bytes;
Christopher Ferrisd0919622016-03-15 22:39:39 -0700411 Header* header;
Christopher Ferris4da25032018-03-07 13:38:48 -0800412 if (g_debug->HeaderEnabled()) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700413 header = g_debug->GetHeader(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800414 free_pointer = header->orig_pointer;
415
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700416 if (g_debug->config().options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700417 if (!g_debug->front_guard->Valid(header)) {
418 g_debug->front_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800419 }
420 }
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700421 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700422 if (!g_debug->rear_guard->Valid(header)) {
423 g_debug->rear_guard->LogFailure(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800424 }
425 }
426
Christopher Ferris7993b802016-01-28 18:35:05 -0800427 header->tag = DEBUG_FREE_TAG;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800428
429 bytes = header->usable_size;
430 } else {
431 bytes = g_dispatch->malloc_usable_size(pointer);
432 }
433
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700434 if (g_debug->config().options() & FILL_ON_FREE) {
435 size_t fill_bytes = g_debug->config().fill_on_free_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800436 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700437 memset(pointer, g_debug->config().fill_free_value(), bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800438 }
439
Christopher Ferris4da25032018-03-07 13:38:48 -0800440 if (g_debug->TrackPointers()) {
441 PointerData::Remove(pointer);
442 }
443
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700444 if (g_debug->config().options() & FREE_TRACK) {
Christopher Ferrisd0919622016-03-15 22:39:39 -0700445 // Do not add the allocation until we are done modifying the pointer
446 // itself. This avoids a race if a lot of threads are all doing
447 // frees at the same time and we wind up trying to really free this
448 // pointer from another thread, while still trying to free it in
449 // this function.
Christopher Ferris4da25032018-03-07 13:38:48 -0800450 pointer = PointerData::AddFreed(pointer);
451 if (pointer != nullptr) {
452 if (g_debug->HeaderEnabled()) {
453 pointer = g_debug->GetHeader(pointer)->orig_pointer;
454 }
455 g_dispatch->free(pointer);
456 }
Christopher Ferrisd0919622016-03-15 22:39:39 -0700457 } else {
458 g_dispatch->free(free_pointer);
459 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800460}
461
Christopher Ferris55a89a42016-04-07 17:14:53 -0700462void debug_free(void* pointer) {
463 if (DebugCallsDisabled() || pointer == nullptr) {
464 return g_dispatch->free(pointer);
465 }
466 ScopedDisableDebugCalls disable;
467
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700468 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700469 g_debug->record->AddEntry(new FreeEntry(pointer));
470 }
471
Christopher Ferris4da25032018-03-07 13:38:48 -0800472 if (!VerifyPointer(pointer, "free")) {
473 return;
474 }
475
476 InternalFree(pointer);
Christopher Ferris55a89a42016-04-07 17:14:53 -0700477}
478
Christopher Ferris63860cb2015-11-16 17:30:32 -0800479void* debug_memalign(size_t alignment, size_t bytes) {
480 if (DebugCallsDisabled()) {
481 return g_dispatch->memalign(alignment, bytes);
482 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700483 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800484
Colin Cross9567c7b2016-03-09 17:56:14 -0800485 if (bytes == 0) {
486 bytes = 1;
487 }
488
Christopher Ferris4da25032018-03-07 13:38:48 -0800489 if (bytes > PointerInfoType::MaxSize()) {
490 errno = ENOMEM;
491 return nullptr;
492 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800493
Christopher Ferris4da25032018-03-07 13:38:48 -0800494 void* pointer;
495 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800496 // Make the alignment a power of two.
497 if (!powerof2(alignment)) {
498 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
499 }
Christopher Ferris72df6702016-02-11 15:51:31 -0800500 // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee
Christopher Ferris63860cb2015-11-16 17:30:32 -0800501 // that the header is aligned properly.
Christopher Ferris72df6702016-02-11 15:51:31 -0800502 if (alignment < MINIMUM_ALIGNMENT_BYTES) {
503 alignment = MINIMUM_ALIGNMENT_BYTES;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800504 }
505
506 // We don't have any idea what the natural alignment of
507 // the underlying native allocator is, so we always need to
508 // over allocate.
509 size_t real_size = alignment + bytes + g_debug->extra_bytes();
510 if (real_size < bytes) {
511 // Overflow.
512 errno = ENOMEM;
513 return nullptr;
514 }
515
516 pointer = g_dispatch->malloc(real_size);
517 if (pointer == nullptr) {
518 return nullptr;
519 }
520
521 uintptr_t value = reinterpret_cast<uintptr_t>(pointer) + g_debug->pointer_offset();
522 // Now align the pointer.
523 value += (-value % alignment);
524
525 Header* header = g_debug->GetHeader(reinterpret_cast<void*>(value));
526 pointer = InitHeader(header, pointer, bytes);
527 } else {
528 size_t real_size = bytes + g_debug->extra_bytes();
529 if (real_size < bytes) {
530 // Overflow.
531 errno = ENOMEM;
532 return nullptr;
533 }
534 pointer = g_dispatch->memalign(alignment, real_size);
535 }
536
Christopher Ferris4da25032018-03-07 13:38:48 -0800537 if (pointer != nullptr) {
538 if (g_debug->TrackPointers()) {
539 PointerData::Add(pointer, bytes);
540 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700541
Christopher Ferris4da25032018-03-07 13:38:48 -0800542 if (g_debug->config().options() & FILL_ON_ALLOC) {
543 size_t bytes = InternalMallocUsableSize(pointer);
544 size_t fill_bytes = g_debug->config().fill_on_alloc_bytes();
545 bytes = (bytes < fill_bytes) ? bytes : fill_bytes;
546 memset(pointer, g_debug->config().fill_alloc_value(), bytes);
547 }
548
549 if (g_debug->config().options() & RECORD_ALLOCS) {
550 g_debug->record->AddEntry(new MemalignEntry(pointer, bytes, alignment));
551 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700552 }
553
Christopher Ferris63860cb2015-11-16 17:30:32 -0800554 return pointer;
555}
556
557void* debug_realloc(void* pointer, size_t bytes) {
558 if (DebugCallsDisabled()) {
559 return g_dispatch->realloc(pointer, bytes);
560 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700561 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800562
563 if (pointer == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800564 pointer = InternalMalloc(bytes);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700565 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700566 g_debug->record->AddEntry(new ReallocEntry(pointer, bytes, nullptr));
567 }
568 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800569 }
570
Christopher Ferris4da25032018-03-07 13:38:48 -0800571 if (!VerifyPointer(pointer, "realloc")) {
572 return nullptr;
573 }
574
Christopher Ferris63860cb2015-11-16 17:30:32 -0800575 if (bytes == 0) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700576 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700577 g_debug->record->AddEntry(new ReallocEntry(nullptr, bytes, pointer));
578 }
579
Christopher Ferris4da25032018-03-07 13:38:48 -0800580 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800581 return nullptr;
582 }
583
584 size_t real_size = bytes;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700585 if (g_debug->config().options() & EXPAND_ALLOC) {
586 real_size += g_debug->config().expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800587 if (real_size < bytes) {
588 // Overflow.
589 errno = ENOMEM;
590 return nullptr;
591 }
592 }
593
Christopher Ferris4da25032018-03-07 13:38:48 -0800594 if (bytes > PointerInfoType::MaxSize()) {
595 errno = ENOMEM;
596 return nullptr;
597 }
598
Christopher Ferris63860cb2015-11-16 17:30:32 -0800599 void* new_pointer;
600 size_t prev_size;
Christopher Ferris4da25032018-03-07 13:38:48 -0800601 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800602 // Same size, do nothing.
Christopher Ferris4da25032018-03-07 13:38:48 -0800603 Header* header = g_debug->GetHeader(pointer);
604 if (real_size == header->size) {
605 if (g_debug->TrackPointers()) {
606 // Remove and re-add so that the backtrace is updated.
607 PointerData::Remove(pointer);
608 PointerData::Add(pointer, real_size);
609 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800610 return pointer;
611 }
612
613 // Allocation is shrinking.
614 if (real_size < header->usable_size) {
615 header->size = real_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700616 if (g_debug->config().options() & REAR_GUARD) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800617 // Don't bother allocating a smaller pointer in this case, simply
618 // change the header usable_size and reset the rear guard.
Christopher Ferris4da25032018-03-07 13:38:48 -0800619 header->usable_size = header->size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700620 memset(g_debug->GetRearGuard(header), g_debug->config().rear_guard_value(),
621 g_debug->config().rear_guard_bytes());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800622 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800623 if (g_debug->TrackPointers()) {
624 // Remove and re-add so that the backtrace is updated.
625 PointerData::Remove(pointer);
626 PointerData::Add(pointer, real_size);
627 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800628 return pointer;
629 }
630
631 // Allocate the new size.
Christopher Ferris4da25032018-03-07 13:38:48 -0800632 new_pointer = InternalMalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800633 if (new_pointer == nullptr) {
634 errno = ENOMEM;
635 return nullptr;
636 }
637
638 prev_size = header->usable_size;
639 memcpy(new_pointer, pointer, prev_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800640 InternalFree(pointer);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800641 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800642 if (g_debug->TrackPointers()) {
643 PointerData::Remove(pointer);
644 }
645
Christopher Ferris63860cb2015-11-16 17:30:32 -0800646 prev_size = g_dispatch->malloc_usable_size(pointer);
647 new_pointer = g_dispatch->realloc(pointer, real_size);
648 if (new_pointer == nullptr) {
649 return nullptr;
650 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800651
652 if (g_debug->TrackPointers()) {
653 PointerData::Add(new_pointer, real_size);
654 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800655 }
656
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700657 if (g_debug->config().options() & FILL_ON_ALLOC) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800658 size_t bytes = InternalMallocUsableSize(new_pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700659 if (bytes > g_debug->config().fill_on_alloc_bytes()) {
660 bytes = g_debug->config().fill_on_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -0800661 }
662 if (bytes > prev_size) {
663 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(new_pointer) + prev_size),
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700664 g_debug->config().fill_alloc_value(), bytes - prev_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800665 }
666 }
667
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700668 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700669 g_debug->record->AddEntry(new ReallocEntry(new_pointer, bytes, pointer));
670 }
671
Christopher Ferris63860cb2015-11-16 17:30:32 -0800672 return new_pointer;
673}
674
675void* debug_calloc(size_t nmemb, size_t bytes) {
676 if (DebugCallsDisabled()) {
677 return g_dispatch->calloc(nmemb, bytes);
678 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700679 ScopedDisableDebugCalls disable;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800680
Colin Cross7877df62016-03-10 13:01:27 -0800681 size_t size;
682 if (__builtin_mul_overflow(nmemb, bytes, &size)) {
683 // Overflow
684 errno = ENOMEM;
685 return nullptr;
686 }
687
Colin Cross9567c7b2016-03-09 17:56:14 -0800688 if (size == 0) {
689 size = 1;
690 }
691
Colin Cross7877df62016-03-10 13:01:27 -0800692 size_t real_size;
693 if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800694 // Overflow.
695 errno = ENOMEM;
696 return nullptr;
697 }
698
Christopher Ferris4da25032018-03-07 13:38:48 -0800699 if (real_size > PointerInfoType::MaxSize()) {
700 errno = ENOMEM;
701 return nullptr;
702 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800703
Christopher Ferris4da25032018-03-07 13:38:48 -0800704 void* pointer;
705 if (g_debug->HeaderEnabled()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800706 // Need to guarantee the alignment of the header.
Christopher Ferris4da25032018-03-07 13:38:48 -0800707 Header* header =
708 reinterpret_cast<Header*>(g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800709 if (header == nullptr) {
710 return nullptr;
711 }
712 memset(header, 0, g_dispatch->malloc_usable_size(header));
Christopher Ferris7bd01782016-04-20 12:30:58 -0700713 pointer = InitHeader(header, header, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800714 } else {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700715 pointer = g_dispatch->calloc(1, real_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800716 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800717
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700718 if (g_debug->config().options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -0700719 g_debug->record->AddEntry(new CallocEntry(pointer, bytes, nmemb));
720 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800721
722 if (pointer != nullptr && g_debug->TrackPointers()) {
723 PointerData::Add(pointer, size);
724 }
Christopher Ferris7bd01782016-04-20 12:30:58 -0700725 return pointer;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800726}
727
728struct mallinfo debug_mallinfo() {
729 return g_dispatch->mallinfo();
730}
731
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700732int debug_mallopt(int param, int value) {
733 return g_dispatch->mallopt(param, value);
734}
735
Christopher Ferris6c619a02019-03-01 17:59:51 -0800736int debug_malloc_info(int options, FILE* fp) {
737 if (DebugCallsDisabled() || !g_debug->TrackPointers()) {
738 return g_dispatch->malloc_info(options, fp);
739 }
740
741 MallocXmlElem root(fp, "malloc", "version=\"debug-malloc-1\"");
742 std::vector<ListInfoType> list;
743 PointerData::GetAllocList(&list);
744
745 size_t alloc_num = 0;
746 for (size_t i = 0; i < list.size(); i++) {
747 MallocXmlElem alloc(fp, "allocation", "nr=\"%zu\"", alloc_num);
748
749 size_t total = 1;
750 size_t size = list[i].size;
751 while (i < list.size() - 1 && list[i + 1].size == size) {
752 i++;
753 total++;
754 }
755 MallocXmlElem(fp, "size").Contents("%zu", list[i].size);
756 MallocXmlElem(fp, "total").Contents("%zu", total);
757 alloc_num++;
758 }
759 return 0;
760}
761
Christopher Ferriscae21a92018-02-05 18:14:55 -0800762void* debug_aligned_alloc(size_t alignment, size_t size) {
763 if (DebugCallsDisabled()) {
764 return g_dispatch->aligned_alloc(alignment, size);
765 }
Christopher Ferrisa22f5d52019-03-01 16:40:59 -0800766 if (!powerof2(alignment) || (size % alignment) != 0) {
Christopher Ferriscae21a92018-02-05 18:14:55 -0800767 errno = EINVAL;
768 return nullptr;
769 }
770 return debug_memalign(alignment, size);
771}
772
Christopher Ferris63860cb2015-11-16 17:30:32 -0800773int debug_posix_memalign(void** memptr, size_t alignment, size_t size) {
774 if (DebugCallsDisabled()) {
775 return g_dispatch->posix_memalign(memptr, alignment, size);
776 }
777
Christopher Ferris6c619a02019-03-01 17:59:51 -0800778 if (alignment < sizeof(void*) || !powerof2(alignment)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800779 return EINVAL;
780 }
781 int saved_errno = errno;
782 *memptr = debug_memalign(alignment, size);
783 errno = saved_errno;
784 return (*memptr != nullptr) ? 0 : ENOMEM;
785}
786
Christopher Ferris4da25032018-03-07 13:38:48 -0800787int debug_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t, size_t, void*),
788 void* arg) {
789 if (g_debug->TrackPointers()) {
790 // Since malloc is disabled, don't bother acquiring any locks.
791 for (auto it = PointerData::begin(); it != PointerData::end(); ++it) {
792 callback(it->first, InternalMallocUsableSize(reinterpret_cast<void*>(it->first)), arg);
793 }
794 return 0;
795 }
Colin Cross869691c2016-01-29 12:48:18 -0800796
Christopher Ferris4da25032018-03-07 13:38:48 -0800797 // An option that adds a header will add pointer tracking, so no need to
798 // check if headers are enabled.
799 return g_dispatch->iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800800}
801
802void debug_malloc_disable() {
803 g_dispatch->malloc_disable();
Christopher Ferris4da25032018-03-07 13:38:48 -0800804 if (g_debug->pointer) {
805 g_debug->pointer->PrepareFork();
Colin Cross869691c2016-01-29 12:48:18 -0800806 }
807}
808
809void debug_malloc_enable() {
Christopher Ferris4da25032018-03-07 13:38:48 -0800810 if (g_debug->pointer) {
811 g_debug->pointer->PostForkParent();
Colin Cross869691c2016-01-29 12:48:18 -0800812 }
813 g_dispatch->malloc_enable();
814}
815
Christopher Ferris4da25032018-03-07 13:38:48 -0800816ssize_t debug_malloc_backtrace(void* pointer, uintptr_t* frames, size_t max_frames) {
Colin Cross2d4721c2016-02-02 11:57:54 -0800817 if (DebugCallsDisabled() || pointer == nullptr) {
818 return 0;
819 }
Christopher Ferris55a89a42016-04-07 17:14:53 -0700820 ScopedDisableDebugCalls disable;
Colin Cross2d4721c2016-02-02 11:57:54 -0800821
Christopher Ferris4da25032018-03-07 13:38:48 -0800822 if (!(g_debug->config().options() & BACKTRACE)) {
823 return 0;
Colin Cross2d4721c2016-02-02 11:57:54 -0800824 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800825 return PointerData::GetFrames(pointer, frames, max_frames);
Colin Cross2d4721c2016-02-02 11:57:54 -0800826}
827
Christopher Ferris63860cb2015-11-16 17:30:32 -0800828#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
829void* debug_pvalloc(size_t bytes) {
830 if (DebugCallsDisabled()) {
831 return g_dispatch->pvalloc(bytes);
832 }
833
834 size_t pagesize = getpagesize();
Dan Alberta613d0d2017-10-05 16:39:33 -0700835 size_t size = __BIONIC_ALIGN(bytes, pagesize);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800836 if (size < bytes) {
837 // Overflow
838 errno = ENOMEM;
839 return nullptr;
840 }
841 return debug_memalign(pagesize, size);
842}
843
844void* debug_valloc(size_t size) {
845 if (DebugCallsDisabled()) {
846 return g_dispatch->valloc(size);
847 }
848 return debug_memalign(getpagesize(), size);
849}
850#endif
Christopher Ferris602b88c2017-08-04 13:04:04 -0700851
852static std::mutex g_dump_lock;
853
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700854static void write_dump(FILE* fp) {
855 fprintf(fp, "Android Native Heap Dump v1.2\n\n");
Christopher Ferris602b88c2017-08-04 13:04:04 -0700856
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700857 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "unknown");
858 fprintf(fp, "Build fingerprint: '%s'\n\n", fingerprint.c_str());
Christopher Ferris602b88c2017-08-04 13:04:04 -0700859
Christopher Ferris4da25032018-03-07 13:38:48 -0800860 PointerData::DumpLiveToFile(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700861
862 fprintf(fp, "MAPS\n");
863 std::string content;
864 if (!android::base::ReadFileToString("/proc/self/maps", &content)) {
865 fprintf(fp, "Could not open /proc/self/maps\n");
866 } else {
867 fprintf(fp, "%s", content.c_str());
868 }
869 fprintf(fp, "END\n");
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700870}
871
872bool debug_write_malloc_leak_info(FILE* fp) {
873 ScopedDisableDebugCalls disable;
874
875 std::lock_guard<std::mutex> guard(g_dump_lock);
876
877 if (!(g_debug->config().options() & BACKTRACE)) {
878 return false;
879 }
880
881 write_dump(fp);
Christopher Ferris602b88c2017-08-04 13:04:04 -0700882 return true;
883}
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700884
885void debug_dump_heap(const char* file_name) {
886 ScopedDisableDebugCalls disable;
887
888 std::lock_guard<std::mutex> guard(g_dump_lock);
889
890 FILE* fp = fopen(file_name, "w+e");
891 if (fp == nullptr) {
892 error_log("Unable to create file: %s", file_name);
893 return;
894 }
895
896 error_log("Dumping to file: %s\n", file_name);
897 write_dump(fp);
898 fclose(fp);
899}