1 /*
2 * Copyright (C) 2015 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 <signal.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37
38 #include <mutex>
39 #include <string>
40 #include <unordered_map>
41 #include <utility>
42 #include <vector>
43
44 #include <android-base/stringprintf.h>
45 #include <android-base/thread_annotations.h>
46 #include <demangle.h>
47 #include <private/bionic_macros.h>
48
49 #include "Config.h"
50 #include "DebugData.h"
51 #include "PointerData.h"
52 #include "backtrace.h"
53 #include "debug_log.h"
54 #include "malloc_debug.h"
55 #include "UnwindBacktrace.h"
56
57 std::atomic_uint8_t PointerData::backtrace_enabled_;
58 std::atomic_bool PointerData::backtrace_dump_;
59
60 std::mutex PointerData::pointer_mutex_;
61 std::unordered_map<uintptr_t, PointerInfoType> PointerData::pointers_ GUARDED_BY(
62 PointerData::pointer_mutex_);
63
64 std::mutex PointerData::frame_mutex_;
65 std::unordered_map<FrameKeyType, size_t> PointerData::key_to_index_ GUARDED_BY(
66 PointerData::frame_mutex_);
67 std::unordered_map<size_t, FrameInfoType> PointerData::frames_ GUARDED_BY(PointerData::frame_mutex_);
68 std::unordered_map<size_t, std::vector<unwindstack::LocalFrameData>> PointerData::backtraces_info_ GUARDED_BY(PointerData::frame_mutex_);
69 constexpr size_t kBacktraceEmptyIndex = 1;
70 size_t PointerData::cur_hash_index_ GUARDED_BY(PointerData::frame_mutex_);
71
72 std::mutex PointerData::free_pointer_mutex_;
73 std::deque<FreePointerInfoType> PointerData::free_pointers_ GUARDED_BY(
74 PointerData::free_pointer_mutex_);
75
76 // Buffer to use for comparison.
77 static constexpr size_t kCompareBufferSize = 512 * 1024;
78 static std::vector<uint8_t> g_cmp_mem(0);
79
ToggleBacktraceEnable(int,siginfo_t *,void *)80 static void ToggleBacktraceEnable(int, siginfo_t*, void*) {
81 g_debug->pointer->ToggleBacktraceEnabled();
82 }
83
EnableDump(int,siginfo_t *,void *)84 static void EnableDump(int, siginfo_t*, void*) {
85 g_debug->pointer->EnableDumping();
86 }
87
PointerData(DebugData * debug_data)88 PointerData::PointerData(DebugData* debug_data) : OptionData(debug_data) {}
89
Initialize(const Config & config)90 bool PointerData::Initialize(const Config& config) NO_THREAD_SAFETY_ANALYSIS {
91 pointers_.clear();
92 key_to_index_.clear();
93 frames_.clear();
94 free_pointers_.clear();
95 // A hash index of kBacktraceEmptyIndex indicates that we tried to get
96 // a backtrace, but there was nothing recorded.
97 cur_hash_index_ = kBacktraceEmptyIndex + 1;
98
99 backtrace_enabled_ = config.backtrace_enabled();
100 if (config.backtrace_enable_on_signal()) {
101 struct sigaction64 enable_act = {};
102 enable_act.sa_sigaction = ToggleBacktraceEnable;
103 enable_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
104 if (sigaction64(config.backtrace_signal(), &enable_act, nullptr) != 0) {
105 error_log("Unable to set up backtrace signal enable function: %s", strerror(errno));
106 return false;
107 }
108 if (config.options() & VERBOSE) {
109 info_log("%s: Run: 'kill -%d %d' to enable backtracing.", getprogname(),
110 config.backtrace_signal(), getpid());
111 }
112 }
113
114 if (config.options() & BACKTRACE) {
115 struct sigaction64 act = {};
116 act.sa_sigaction = EnableDump;
117 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
118 if (sigaction64(config.backtrace_dump_signal(), &act, nullptr) != 0) {
119 error_log("Unable to set up backtrace dump signal function: %s", strerror(errno));
120 return false;
121 }
122 if (config.options() & VERBOSE) {
123 info_log("%s: Run: 'kill -%d %d' to dump the backtrace.", getprogname(),
124 config.backtrace_dump_signal(), getpid());
125 }
126 }
127
128 backtrace_dump_ = false;
129
130 if (config.options() & FREE_TRACK) {
131 g_cmp_mem.resize(kCompareBufferSize, config.fill_free_value());
132 }
133 return true;
134 }
135
AddBacktrace(size_t num_frames)136 size_t PointerData::AddBacktrace(size_t num_frames) {
137 std::vector<uintptr_t> frames;
138 std::vector<unwindstack::LocalFrameData> frames_info;
139 if (g_debug->config().options() & BACKTRACE_FULL) {
140 if (!Unwind(&frames, &frames_info, num_frames)) {
141 return kBacktraceEmptyIndex;
142 }
143 } else {
144 frames.resize(num_frames);
145 num_frames = backtrace_get(frames.data(), frames.size());
146 if (num_frames == 0) {
147 return kBacktraceEmptyIndex;
148 }
149 }
150
151 FrameKeyType key{.num_frames = num_frames, .frames = frames.data()};
152 size_t hash_index;
153 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
154 auto entry = key_to_index_.find(key);
155 if (entry == key_to_index_.end()) {
156 frames.resize(num_frames);
157 hash_index = cur_hash_index_++;
158 key.frames = frames.data();
159 key_to_index_.emplace(key, hash_index);
160
161 frames_.emplace(hash_index, FrameInfoType{.references = 1, .frames = std::move(frames)});
162 if (g_debug->config().options() & BACKTRACE_FULL) {
163 backtraces_info_.emplace(hash_index, std::move(frames_info));
164 }
165 } else {
166 hash_index = entry->second;
167 FrameInfoType* frame_info = &frames_[hash_index];
168 frame_info->references++;
169 }
170 return hash_index;
171 }
172
RemoveBacktrace(size_t hash_index)173 void PointerData::RemoveBacktrace(size_t hash_index) {
174 if (hash_index <= kBacktraceEmptyIndex) {
175 return;
176 }
177
178 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
179 auto frame_entry = frames_.find(hash_index);
180 if (frame_entry == frames_.end()) {
181 error_log("hash_index %zu does not have matching frame data.", hash_index);
182 return;
183 }
184 FrameInfoType* frame_info = &frame_entry->second;
185 if (--frame_info->references == 0) {
186 FrameKeyType key{.num_frames = frame_info->frames.size(), .frames = frame_info->frames.data()};
187 key_to_index_.erase(key);
188 frames_.erase(hash_index);
189 if (g_debug->config().options() & BACKTRACE_FULL) {
190 backtraces_info_.erase(hash_index);
191 }
192 }
193 }
194
Add(const void * ptr,size_t pointer_size)195 void PointerData::Add(const void* ptr, size_t pointer_size) {
196 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
197 size_t hash_index = 0;
198 if (backtrace_enabled_) {
199 hash_index = AddBacktrace(g_debug->config().backtrace_frames());
200 }
201
202 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
203 pointers_[pointer] = PointerInfoType{PointerInfoType::GetEncodedSize(pointer_size), hash_index};
204 }
205
Remove(const void * ptr)206 void PointerData::Remove(const void* ptr) {
207 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
208 size_t hash_index;
209 {
210 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
211 auto entry = pointers_.find(pointer);
212 if (entry == pointers_.end()) {
213 // Attempt to remove unknown pointer.
214 error_log("No tracked pointer found for 0x%" PRIxPTR, pointer);
215 return;
216 }
217 hash_index = entry->second.hash_index;
218 pointers_.erase(pointer);
219 }
220
221 RemoveBacktrace(hash_index);
222 }
223
GetFrames(const void * ptr,uintptr_t * frames,size_t max_frames)224 size_t PointerData::GetFrames(const void* ptr, uintptr_t* frames, size_t max_frames) {
225 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
226 size_t hash_index;
227 {
228 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
229 auto entry = pointers_.find(pointer);
230 if (entry == pointers_.end()) {
231 return 0;
232 }
233 hash_index = entry->second.hash_index;
234 }
235
236 if (hash_index <= kBacktraceEmptyIndex) {
237 return 0;
238 }
239
240 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
241 auto frame_entry = frames_.find(hash_index);
242 if (frame_entry == frames_.end()) {
243 return 0;
244 }
245 FrameInfoType* frame_info = &frame_entry->second;
246 if (max_frames > frame_info->frames.size()) {
247 max_frames = frame_info->frames.size();
248 }
249 memcpy(frames, &frame_info->frames[0], max_frames * sizeof(uintptr_t));
250
251 return max_frames;
252 }
253
LogBacktrace(size_t hash_index)254 void PointerData::LogBacktrace(size_t hash_index) {
255 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
256 if (g_debug->config().options() & BACKTRACE_FULL) {
257 auto backtrace_info_entry = backtraces_info_.find(hash_index);
258 if (backtrace_info_entry != backtraces_info_.end()) {
259 UnwindLog(backtrace_info_entry->second);
260 return;
261 }
262 } else {
263 auto frame_entry = frames_.find(hash_index);
264 if (frame_entry != frames_.end()) {
265 FrameInfoType* frame_info = &frame_entry->second;
266 backtrace_log(frame_info->frames.data(), frame_info->frames.size());
267 return;
268 }
269 }
270 error_log(" hash_index %zu does not have matching frame data.", hash_index);
271 }
272
LogFreeError(const FreePointerInfoType & info,size_t max_cmp_bytes)273 void PointerData::LogFreeError(const FreePointerInfoType& info, size_t max_cmp_bytes) {
274 error_log(LOG_DIVIDER);
275 uint8_t* memory = reinterpret_cast<uint8_t*>(info.pointer);
276 error_log("+++ ALLOCATION %p USED AFTER FREE", memory);
277 uint8_t fill_free_value = g_debug->config().fill_free_value();
278 for (size_t i = 0; i < max_cmp_bytes; i++) {
279 if (memory[i] != fill_free_value) {
280 error_log(" allocation[%zu] = 0x%02x (expected 0x%02x)", i, memory[i], fill_free_value);
281 }
282 }
283
284 if (info.hash_index > kBacktraceEmptyIndex) {
285 error_log("Backtrace at time of free:");
286 LogBacktrace(info.hash_index);
287 }
288
289 error_log(LOG_DIVIDER);
290 if (g_debug->config().options() & ABORT_ON_ERROR) {
291 abort();
292 }
293 }
294
VerifyFreedPointer(const FreePointerInfoType & info)295 void PointerData::VerifyFreedPointer(const FreePointerInfoType& info) {
296 size_t usable_size;
297 if (g_debug->HeaderEnabled()) {
298 // Check to see if the tag data has been damaged.
299 Header* header = g_debug->GetHeader(reinterpret_cast<const void*>(info.pointer));
300 if (header->tag != DEBUG_FREE_TAG) {
301 error_log(LOG_DIVIDER);
302 error_log("+++ ALLOCATION 0x%" PRIxPTR " HAS CORRUPTED HEADER TAG 0x%x AFTER FREE",
303 info.pointer, header->tag);
304 error_log(LOG_DIVIDER);
305 if (g_debug->config().options() & ABORT_ON_ERROR) {
306 abort();
307 }
308
309 // Stop processing here, it is impossible to tell how the header
310 // may have been damaged.
311 return;
312 }
313 usable_size = header->usable_size;
314 } else {
315 usable_size = g_dispatch->malloc_usable_size(reinterpret_cast<const void*>(info.pointer));
316 }
317
318 size_t bytes = (usable_size < g_debug->config().fill_on_free_bytes())
319 ? usable_size
320 : g_debug->config().fill_on_free_bytes();
321 size_t max_cmp_bytes = bytes;
322 const uint8_t* memory = reinterpret_cast<const uint8_t*>(info.pointer);
323 while (bytes > 0) {
324 size_t bytes_to_cmp = (bytes < g_cmp_mem.size()) ? bytes : g_cmp_mem.size();
325 if (memcmp(memory, g_cmp_mem.data(), bytes_to_cmp) != 0) {
326 LogFreeError(info, max_cmp_bytes);
327 }
328 bytes -= bytes_to_cmp;
329 memory = &memory[bytes_to_cmp];
330 }
331 }
332
AddFreed(const void * ptr)333 void* PointerData::AddFreed(const void* ptr) {
334 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
335
336 size_t hash_index = 0;
337 size_t num_frames = g_debug->config().free_track_backtrace_num_frames();
338 if (num_frames) {
339 hash_index = AddBacktrace(num_frames);
340 }
341
342 void* last = nullptr;
343 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
344 if (free_pointers_.size() == g_debug->config().free_track_allocations()) {
345 FreePointerInfoType info(free_pointers_.front());
346 free_pointers_.pop_front();
347 VerifyFreedPointer(info);
348 RemoveBacktrace(info.hash_index);
349 last = reinterpret_cast<void*>(info.pointer);
350 }
351
352 free_pointers_.emplace_back(FreePointerInfoType{pointer, hash_index});
353 return last;
354 }
355
LogFreeBacktrace(const void * ptr)356 void PointerData::LogFreeBacktrace(const void* ptr) {
357 size_t hash_index = 0;
358 {
359 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
360 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
361 for (const auto& info : free_pointers_) {
362 if (info.pointer == pointer) {
363 hash_index = info.hash_index;
364 break;
365 }
366 }
367 }
368
369 if (hash_index <= kBacktraceEmptyIndex) {
370 return;
371 }
372
373 error_log("Backtrace of original free:");
374 LogBacktrace(hash_index);
375 }
376
VerifyAllFreed()377 void PointerData::VerifyAllFreed() {
378 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
379 for (auto& free_info : free_pointers_) {
380 VerifyFreedPointer(free_info);
381 }
382 }
383
GetList(std::vector<ListInfoType> * list,bool only_with_backtrace)384 void PointerData::GetList(std::vector<ListInfoType>* list, bool only_with_backtrace)
385 REQUIRES(pointer_mutex_, frame_mutex_) {
386 for (const auto& entry : pointers_) {
387 FrameInfoType* frame_info = nullptr;
388 std::vector<unwindstack::LocalFrameData>* backtrace_info = nullptr;
389 size_t hash_index = entry.second.hash_index;
390 if (hash_index > kBacktraceEmptyIndex) {
391 auto frame_entry = frames_.find(hash_index);
392 if (frame_entry == frames_.end()) {
393 // Somehow wound up with a pointer with a valid hash_index, but
394 // no frame data. This should not be possible since adding a pointer
395 // occurs after the hash_index and frame data have been added.
396 // When removing a pointer, the pointer is deleted before the frame
397 // data.
398 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", entry.first, hash_index);
399 } else {
400 frame_info = &frame_entry->second;
401 }
402
403 if (g_debug->config().options() & BACKTRACE_FULL) {
404 auto backtrace_entry = backtraces_info_.find(hash_index);
405 if (backtrace_entry == backtraces_info_.end()) {
406 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", entry.first, hash_index);
407 } else {
408 backtrace_info = &backtrace_entry->second;
409 }
410 }
411 }
412 if (hash_index == 0 && only_with_backtrace) {
413 continue;
414 }
415
416 list->emplace_back(ListInfoType{entry.first, 1, entry.second.RealSize(),
417 entry.second.ZygoteChildAlloc(), frame_info, backtrace_info});
418 }
419
420 // Sort by the size of the allocation.
421 std::sort(list->begin(), list->end(), [](const ListInfoType& a, const ListInfoType& b) {
422 // Put zygote child allocations first.
423 bool a_zygote_child_alloc = a.zygote_child_alloc;
424 bool b_zygote_child_alloc = b.zygote_child_alloc;
425 if (a_zygote_child_alloc && !b_zygote_child_alloc) {
426 return false;
427 }
428 if (!a_zygote_child_alloc && b_zygote_child_alloc) {
429 return true;
430 }
431
432 // Sort by size, descending order.
433 if (a.size != b.size) return a.size > b.size;
434
435 // Put pointers with no backtrace last.
436 FrameInfoType* a_frame = a.frame_info;
437 FrameInfoType* b_frame = b.frame_info;
438 if (a_frame == nullptr && b_frame != nullptr) {
439 return false;
440 } else if (a_frame != nullptr && b_frame == nullptr) {
441 return true;
442 } else if (a_frame == nullptr && b_frame == nullptr) {
443 return a.pointer < b.pointer;
444 }
445
446 // Put the pointers with longest backtrace first.
447 if (a_frame->frames.size() != b_frame->frames.size()) {
448 return a_frame->frames.size() > b_frame->frames.size();
449 }
450
451 // Last sort by pointer.
452 return a.pointer < b.pointer;
453 });
454 }
455
GetUniqueList(std::vector<ListInfoType> * list,bool only_with_backtrace)456 void PointerData::GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace)
457 REQUIRES(pointer_mutex_, frame_mutex_) {
458 GetList(list, only_with_backtrace);
459
460 // Remove duplicates of size/backtraces.
461 for (auto iter = list->begin(); iter != list->end();) {
462 auto dup_iter = iter + 1;
463 bool zygote_child_alloc = iter->zygote_child_alloc;
464 size_t size = iter->size;
465 FrameInfoType* frame_info = iter->frame_info;
466 for (; dup_iter != list->end(); ++dup_iter) {
467 if (zygote_child_alloc != dup_iter->zygote_child_alloc || size != dup_iter->size ||
468 frame_info != dup_iter->frame_info) {
469 break;
470 }
471 iter->num_allocations++;
472 }
473 iter = list->erase(iter + 1, dup_iter);
474 }
475 }
476
LogLeaks()477 void PointerData::LogLeaks() {
478 std::vector<ListInfoType> list;
479
480 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
481 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
482 GetList(&list, false);
483
484 size_t track_count = 0;
485 for (const auto& list_info : list) {
486 error_log("+++ %s leaked block of size %zu at 0x%" PRIxPTR " (leak %zu of %zu)", getprogname(),
487 list_info.size, list_info.pointer, ++track_count, list.size());
488 if (list_info.backtrace_info != nullptr) {
489 error_log("Backtrace at time of allocation:");
490 UnwindLog(*list_info.backtrace_info);
491 } else if (list_info.frame_info != nullptr) {
492 error_log("Backtrace at time of allocation:");
493 backtrace_log(list_info.frame_info->frames.data(), list_info.frame_info->frames.size());
494 }
495 // Do not bother to free the pointers, we are about to exit any way.
496 }
497 }
498
GetAllocList(std::vector<ListInfoType> * list)499 void PointerData::GetAllocList(std::vector<ListInfoType>* list) {
500 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
501 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
502
503 if (pointers_.empty()) {
504 return;
505 }
506
507 GetList(list, false);
508 }
509
GetInfo(uint8_t ** info,size_t * overall_size,size_t * info_size,size_t * total_memory,size_t * backtrace_size)510 void PointerData::GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size,
511 size_t* total_memory, size_t* backtrace_size) {
512 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
513 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
514
515 if (pointers_.empty()) {
516 return;
517 }
518
519 std::vector<ListInfoType> list;
520 GetUniqueList(&list, true);
521 if (list.empty()) {
522 return;
523 }
524
525 *backtrace_size = g_debug->config().backtrace_frames();
526 *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size;
527 *overall_size = *info_size * list.size();
528 *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, list.size()));
529 if (*info == nullptr) {
530 return;
531 }
532
533 uint8_t* data = *info;
534 *total_memory = 0;
535 for (const auto& list_info : list) {
536 FrameInfoType* frame_info = list_info.frame_info;
537 *total_memory += list_info.size * list_info.num_allocations;
538 size_t allocation_size =
539 PointerInfoType::GetEncodedSize(list_info.zygote_child_alloc, list_info.size);
540 memcpy(data, &allocation_size, sizeof(size_t));
541 memcpy(&data[sizeof(size_t)], &list_info.num_allocations, sizeof(size_t));
542 if (frame_info != nullptr) {
543 memcpy(&data[2 * sizeof(size_t)], frame_info->frames.data(),
544 frame_info->frames.size() * sizeof(uintptr_t));
545 }
546 data += *info_size;
547 }
548 }
549
Exists(const void * ptr)550 bool PointerData::Exists(const void* ptr) {
551 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
552 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
553 return pointers_.count(pointer) != 0;
554 }
555
DumpLiveToFile(FILE * fp)556 void PointerData::DumpLiveToFile(FILE* fp) {
557 std::vector<ListInfoType> list;
558
559 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
560 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
561 GetUniqueList(&list, false);
562
563 size_t total_memory = 0;
564 for (const auto& info : list) {
565 total_memory += info.size * info.num_allocations;
566 }
567
568 fprintf(fp, "Total memory: %zu\n", total_memory);
569 fprintf(fp, "Allocation records: %zd\n", list.size());
570 fprintf(fp, "Backtrace size: %zu\n", g_debug->config().backtrace_frames());
571 fprintf(fp, "\n");
572
573 for (const auto& info : list) {
574 fprintf(fp, "z %d sz %8zu num %zu bt", (info.zygote_child_alloc) ? 1 : 0, info.size,
575 info.num_allocations);
576 FrameInfoType* frame_info = info.frame_info;
577 if (frame_info != nullptr) {
578 for (size_t i = 0; i < frame_info->frames.size(); i++) {
579 if (frame_info->frames[i] == 0) {
580 break;
581 }
582 fprintf(fp, " %" PRIxPTR, frame_info->frames[i]);
583 }
584 }
585 fprintf(fp, "\n");
586 if (info.backtrace_info != nullptr) {
587 fprintf(fp, " bt_info");
588 for (const auto& frame : *info.backtrace_info) {
589 fprintf(fp, " {");
590 if (frame.map_info != nullptr && !frame.map_info->name.empty()) {
591 fprintf(fp, "\"%s\"", frame.map_info->name.c_str());
592 } else {
593 fprintf(fp, "\"\"");
594 }
595 fprintf(fp, " %" PRIx64, frame.rel_pc);
596 if (frame.function_name.empty()) {
597 fprintf(fp, " \"\" 0}");
598 } else {
599 fprintf(fp, " \"%s\" %" PRIx64 "}", demangle(frame.function_name.c_str()).c_str(), frame.function_offset);
600 }
601 }
602 fprintf(fp, "\n");
603 }
604 }
605 }
606
PrepareFork()607 void PointerData::PrepareFork() NO_THREAD_SAFETY_ANALYSIS {
608 free_pointer_mutex_.lock();
609 pointer_mutex_.lock();
610 frame_mutex_.lock();
611 }
612
PostForkParent()613 void PointerData::PostForkParent() NO_THREAD_SAFETY_ANALYSIS {
614 frame_mutex_.unlock();
615 pointer_mutex_.unlock();
616 free_pointer_mutex_.unlock();
617 }
618
PostForkChild()619 void PointerData::PostForkChild() __attribute__((no_thread_safety_analysis)) {
620 // Make sure that any potential mutexes have been released and are back
621 // to an initial state.
622 frame_mutex_.try_lock();
623 frame_mutex_.unlock();
624 pointer_mutex_.try_lock();
625 pointer_mutex_.unlock();
626 free_pointer_mutex_.try_lock();
627 free_pointer_mutex_.unlock();
628 }
629