blob: 870cd1d16b6c2bd0a0c66f1adb5b37b54f7c32f2 [file] [log] [blame]
Colin Cross7add50d2016-01-14 15:35:40 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <inttypes.h>
18
19#include <functional>
20#include <iomanip>
21#include <mutex>
Colin Cross7add50d2016-01-14 15:35:40 -080022#include <sstream>
Colin Crossa83881e2017-06-22 10:50:05 -070023#include <string>
Colin Cross7a22e812016-03-04 16:36:12 -080024#include <unordered_map>
Colin Cross7add50d2016-01-14 15:35:40 -080025
Colin Cross7add50d2016-01-14 15:35:40 -080026#include <android-base/macros.h>
Colin Crossa83881e2017-06-22 10:50:05 -070027#include <backtrace.h>
Colin Cross7add50d2016-01-14 15:35:40 -080028
29#include "Allocator.h"
30#include "HeapWalker.h"
Colin Cross7a22e812016-03-04 16:36:12 -080031#include "Leak.h"
Colin Cross8e8f34c2016-03-02 17:53:39 -080032#include "LeakFolding.h"
Colin Cross7add50d2016-01-14 15:35:40 -080033#include "LeakPipe.h"
34#include "ProcessMappings.h"
35#include "PtracerThread.h"
36#include "ScopedDisableMalloc.h"
37#include "Semaphore.h"
38#include "ThreadCapture.h"
39
Colin Cross7add50d2016-01-14 15:35:40 -080040#include "bionic.h"
41#include "log.h"
Colin Crossa83881e2017-06-22 10:50:05 -070042#include "memunreachable/memunreachable.h"
Colin Cross7add50d2016-01-14 15:35:40 -080043
44const size_t Leak::contents_length;
45
46using namespace std::chrono_literals;
47
48class MemUnreachable {
49 public:
Colin Crossa83881e2017-06-22 10:50:05 -070050 MemUnreachable(pid_t pid, Allocator<void> allocator)
51 : pid_(pid), allocator_(allocator), heap_walker_(allocator_) {}
Colin Cross7add50d2016-01-14 15:35:40 -080052 bool CollectAllocations(const allocator::vector<ThreadInfo>& threads,
Colin Crossa83881e2017-06-22 10:50:05 -070053 const allocator::vector<Mapping>& mappings);
54 bool GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit, size_t* num_leaks,
55 size_t* leak_bytes);
Colin Cross7add50d2016-01-14 15:35:40 -080056 size_t Allocations() { return heap_walker_.Allocations(); }
57 size_t AllocationBytes() { return heap_walker_.AllocationBytes(); }
Colin Crossa83881e2017-06-22 10:50:05 -070058
Colin Cross7add50d2016-01-14 15:35:40 -080059 private:
60 bool ClassifyMappings(const allocator::vector<Mapping>& mappings,
Colin Crossa83881e2017-06-22 10:50:05 -070061 allocator::vector<Mapping>& heap_mappings,
62 allocator::vector<Mapping>& anon_mappings,
63 allocator::vector<Mapping>& globals_mappings,
64 allocator::vector<Mapping>& stack_mappings);
Colin Cross7add50d2016-01-14 15:35:40 -080065 DISALLOW_COPY_AND_ASSIGN(MemUnreachable);
66 pid_t pid_;
67 Allocator<void> allocator_;
68 HeapWalker heap_walker_;
69};
70
71static void HeapIterate(const Mapping& heap_mapping,
Colin Crossa83881e2017-06-22 10:50:05 -070072 const std::function<void(uintptr_t, size_t)>& func) {
Colin Cross7add50d2016-01-14 15:35:40 -080073 malloc_iterate(heap_mapping.begin, heap_mapping.end - heap_mapping.begin,
Colin Crossa83881e2017-06-22 10:50:05 -070074 [](uintptr_t base, size_t size, void* arg) {
75 auto f = reinterpret_cast<const std::function<void(uintptr_t, size_t)>*>(arg);
76 (*f)(base, size);
77 },
78 const_cast<void*>(reinterpret_cast<const void*>(&func)));
Colin Cross7add50d2016-01-14 15:35:40 -080079}
80
81bool MemUnreachable::CollectAllocations(const allocator::vector<ThreadInfo>& threads,
Colin Crossa83881e2017-06-22 10:50:05 -070082 const allocator::vector<Mapping>& mappings) {
Christopher Ferris47dea712017-05-03 17:34:29 -070083 MEM_ALOGI("searching process %d for allocations", pid_);
Colin Cross7add50d2016-01-14 15:35:40 -080084 allocator::vector<Mapping> heap_mappings{mappings};
85 allocator::vector<Mapping> anon_mappings{mappings};
86 allocator::vector<Mapping> globals_mappings{mappings};
87 allocator::vector<Mapping> stack_mappings{mappings};
Christopher Ferris47dea712017-05-03 17:34:29 -070088 if (!ClassifyMappings(mappings, heap_mappings, anon_mappings, globals_mappings, stack_mappings)) {
Colin Cross7add50d2016-01-14 15:35:40 -080089 return false;
90 }
91
92 for (auto it = heap_mappings.begin(); it != heap_mappings.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -070093 MEM_ALOGV("Heap mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
94 HeapIterate(*it,
95 [&](uintptr_t base, size_t size) { heap_walker_.Allocation(base, base + size); });
Colin Cross7add50d2016-01-14 15:35:40 -080096 }
97
98 for (auto it = anon_mappings.begin(); it != anon_mappings.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -070099 MEM_ALOGV("Anon mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
Colin Cross7add50d2016-01-14 15:35:40 -0800100 heap_walker_.Allocation(it->begin, it->end);
101 }
102
103 for (auto it = globals_mappings.begin(); it != globals_mappings.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700104 MEM_ALOGV("Globals mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
Colin Cross7add50d2016-01-14 15:35:40 -0800105 heap_walker_.Root(it->begin, it->end);
106 }
107
108 for (auto thread_it = threads.begin(); thread_it != threads.end(); thread_it++) {
109 for (auto it = stack_mappings.begin(); it != stack_mappings.end(); it++) {
110 if (thread_it->stack.first >= it->begin && thread_it->stack.first <= it->end) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700111 MEM_ALOGV("Stack %" PRIxPTR "-%" PRIxPTR " %s", thread_it->stack.first, it->end, it->name);
Colin Cross7add50d2016-01-14 15:35:40 -0800112 heap_walker_.Root(thread_it->stack.first, it->end);
113 }
114 }
115 heap_walker_.Root(thread_it->regs);
116 }
117
Christopher Ferris47dea712017-05-03 17:34:29 -0700118 MEM_ALOGI("searching done");
Colin Cross7add50d2016-01-14 15:35:40 -0800119
120 return true;
121}
122
Colin Crossa83881e2017-06-22 10:50:05 -0700123bool MemUnreachable::GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit,
124 size_t* num_leaks, size_t* leak_bytes) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700125 MEM_ALOGI("sweeping process %d for unreachable memory", pid_);
Colin Cross7add50d2016-01-14 15:35:40 -0800126 leaks.clear();
127
Colin Cross8e8f34c2016-03-02 17:53:39 -0800128 if (!heap_walker_.DetectLeaks()) {
129 return false;
130 }
131
Colin Cross7a22e812016-03-04 16:36:12 -0800132 allocator::vector<Range> leaked1{allocator_};
133 heap_walker_.Leaked(leaked1, 0, num_leaks, leak_bytes);
134
Christopher Ferris47dea712017-05-03 17:34:29 -0700135 MEM_ALOGI("sweeping done");
Colin Cross7a22e812016-03-04 16:36:12 -0800136
Christopher Ferris47dea712017-05-03 17:34:29 -0700137 MEM_ALOGI("folding related leaks");
Colin Cross7a22e812016-03-04 16:36:12 -0800138
Colin Cross8e8f34c2016-03-02 17:53:39 -0800139 LeakFolding folding(allocator_, heap_walker_);
140 if (!folding.FoldLeaks()) {
141 return false;
142 }
143
144 allocator::vector<LeakFolding::Leak> leaked{allocator_};
145
Colin Cross7a22e812016-03-04 16:36:12 -0800146 if (!folding.Leaked(leaked, num_leaks, leak_bytes)) {
Colin Cross7add50d2016-01-14 15:35:40 -0800147 return false;
148 }
149
Colin Cross7a22e812016-03-04 16:36:12 -0800150 allocator::unordered_map<Leak::Backtrace, Leak*> backtrace_map{allocator_};
151
152 // Prevent reallocations of backing memory so we can store pointers into it
153 // in backtrace_map.
154 leaks.reserve(leaked.size());
155
Colin Crossa83881e2017-06-22 10:50:05 -0700156 for (auto& it : leaked) {
Colin Cross7a22e812016-03-04 16:36:12 -0800157 leaks.emplace_back();
158 Leak* leak = &leaks.back();
159
Colin Crossa83881e2017-06-22 10:50:05 -0700160 ssize_t num_backtrace_frames = malloc_backtrace(
161 reinterpret_cast<void*>(it.range.begin), leak->backtrace.frames, leak->backtrace.max_frames);
Colin Cross7add50d2016-01-14 15:35:40 -0800162 if (num_backtrace_frames > 0) {
Colin Cross7a22e812016-03-04 16:36:12 -0800163 leak->backtrace.num_frames = num_backtrace_frames;
164
165 auto inserted = backtrace_map.emplace(leak->backtrace, leak);
166 if (!inserted.second) {
167 // Leak with same backtrace already exists, drop this one and
168 // increment similar counts on the existing one.
169 leaks.pop_back();
170 Leak* similar_leak = inserted.first->second;
171 similar_leak->similar_count++;
172 similar_leak->similar_size += it.range.size();
173 similar_leak->similar_referenced_count += it.referenced_count;
174 similar_leak->similar_referenced_size += it.referenced_size;
175 similar_leak->total_size += it.range.size();
176 similar_leak->total_size += it.referenced_size;
177 continue;
178 }
Colin Cross7add50d2016-01-14 15:35:40 -0800179 }
Colin Cross7a22e812016-03-04 16:36:12 -0800180
181 leak->begin = it.range.begin;
182 leak->size = it.range.size();
183 leak->referenced_count = it.referenced_count;
184 leak->referenced_size = it.referenced_size;
185 leak->total_size = leak->size + leak->referenced_size;
186 memcpy(leak->contents, reinterpret_cast<void*>(it.range.begin),
Colin Crossa83881e2017-06-22 10:50:05 -0700187 std::min(leak->size, Leak::contents_length));
Colin Cross7add50d2016-01-14 15:35:40 -0800188 }
189
Christopher Ferris47dea712017-05-03 17:34:29 -0700190 MEM_ALOGI("folding done");
Colin Cross7a22e812016-03-04 16:36:12 -0800191
Colin Crossa83881e2017-06-22 10:50:05 -0700192 std::sort(leaks.begin(), leaks.end(),
193 [](const Leak& a, const Leak& b) { return a.total_size > b.total_size; });
Colin Cross7a22e812016-03-04 16:36:12 -0800194
195 if (leaks.size() > limit) {
196 leaks.resize(limit);
197 }
Colin Cross7add50d2016-01-14 15:35:40 -0800198
199 return true;
200}
201
202static bool has_prefix(const allocator::string& s, const char* prefix) {
203 int ret = s.compare(0, strlen(prefix), prefix);
204 return ret == 0;
205}
206
207bool MemUnreachable::ClassifyMappings(const allocator::vector<Mapping>& mappings,
Colin Crossa83881e2017-06-22 10:50:05 -0700208 allocator::vector<Mapping>& heap_mappings,
209 allocator::vector<Mapping>& anon_mappings,
210 allocator::vector<Mapping>& globals_mappings,
211 allocator::vector<Mapping>& stack_mappings) {
Colin Cross7add50d2016-01-14 15:35:40 -0800212 heap_mappings.clear();
213 anon_mappings.clear();
214 globals_mappings.clear();
215 stack_mappings.clear();
216
217 allocator::string current_lib{allocator_};
218
219 for (auto it = mappings.begin(); it != mappings.end(); it++) {
220 if (it->execute) {
221 current_lib = it->name;
222 continue;
223 }
224
225 if (!it->read) {
226 continue;
227 }
228
229 const allocator::string mapping_name{it->name, allocator_};
230 if (mapping_name == "[anon:.bss]") {
231 // named .bss section
232 globals_mappings.emplace_back(*it);
233 } else if (mapping_name == current_lib) {
234 // .rodata or .data section
235 globals_mappings.emplace_back(*it);
236 } else if (mapping_name == "[anon:libc_malloc]") {
237 // named malloc mapping
238 heap_mappings.emplace_back(*it);
239 } else if (has_prefix(mapping_name, "/dev/ashmem/dalvik")) {
240 // named dalvik heap mapping
241 globals_mappings.emplace_back(*it);
242 } else if (has_prefix(mapping_name, "[stack")) {
243 // named stack mapping
244 stack_mappings.emplace_back(*it);
245 } else if (mapping_name.size() == 0) {
246 globals_mappings.emplace_back(*it);
Colin Crossa83881e2017-06-22 10:50:05 -0700247 } else if (has_prefix(mapping_name, "[anon:") &&
248 mapping_name != "[anon:leak_detector_malloc]") {
Colin Cross7add50d2016-01-14 15:35:40 -0800249 // TODO(ccross): it would be nice to treat named anonymous mappings as
250 // possible leaks, but naming something in a .bss or .data section makes
251 // it impossible to distinguish them from mmaped and then named mappings.
252 globals_mappings.emplace_back(*it);
253 }
254 }
255
256 return true;
257}
258
Colin Crossa83881e2017-06-22 10:50:05 -0700259template <typename T>
Colin Cross7a22e812016-03-04 16:36:12 -0800260static inline const char* plural(T val) {
261 return (val == 1) ? "" : "s";
262}
263
Colin Cross7add50d2016-01-14 15:35:40 -0800264bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit) {
265 int parent_pid = getpid();
266 int parent_tid = gettid();
267
268 Heap heap;
269
270 Semaphore continue_parent_sem;
271 LeakPipe pipe;
272
273 PtracerThread thread{[&]() -> int {
274 /////////////////////////////////////////////
275 // Collection thread
276 /////////////////////////////////////////////
Christopher Ferris47dea712017-05-03 17:34:29 -0700277 MEM_ALOGI("collecting thread info for process %d...", parent_pid);
Colin Cross7add50d2016-01-14 15:35:40 -0800278
279 ThreadCapture thread_capture(parent_pid, heap);
280 allocator::vector<ThreadInfo> thread_info(heap);
281 allocator::vector<Mapping> mappings(heap);
282
283 // ptrace all the threads
284 if (!thread_capture.CaptureThreads()) {
Colin Crossde42af02016-01-14 15:35:40 -0800285 continue_parent_sem.Post();
Colin Cross7add50d2016-01-14 15:35:40 -0800286 return 1;
287 }
288
289 // collect register contents and stacks
290 if (!thread_capture.CapturedThreadInfo(thread_info)) {
Colin Crossde42af02016-01-14 15:35:40 -0800291 continue_parent_sem.Post();
Colin Cross7add50d2016-01-14 15:35:40 -0800292 return 1;
293 }
294
295 // snapshot /proc/pid/maps
296 if (!ProcessMappings(parent_pid, mappings)) {
Colin Crossde42af02016-01-14 15:35:40 -0800297 continue_parent_sem.Post();
Colin Cross7add50d2016-01-14 15:35:40 -0800298 return 1;
299 }
300
301 // malloc must be enabled to call fork, at_fork handlers take the same
302 // locks as ScopedDisableMalloc. All threads are paused in ptrace, so
303 // memory state is still consistent. Unfreeze the original thread so it
304 // can drop the malloc locks, it will block until the collection thread
305 // exits.
306 thread_capture.ReleaseThread(parent_tid);
307 continue_parent_sem.Post();
308
309 // fork a process to do the heap walking
310 int ret = fork();
311 if (ret < 0) {
312 return 1;
313 } else if (ret == 0) {
314 /////////////////////////////////////////////
315 // Heap walker process
316 /////////////////////////////////////////////
317 // Examine memory state in the child using the data collected above and
318 // the CoW snapshot of the process memory contents.
319
320 if (!pipe.OpenSender()) {
321 _exit(1);
322 }
323
324 MemUnreachable unreachable{parent_pid, heap};
325
326 if (!unreachable.CollectAllocations(thread_info, mappings)) {
327 _exit(2);
328 }
329 size_t num_allocations = unreachable.Allocations();
330 size_t allocation_bytes = unreachable.AllocationBytes();
331
332 allocator::vector<Leak> leaks{heap};
333
334 size_t num_leaks = 0;
335 size_t leak_bytes = 0;
336 bool ok = unreachable.GetUnreachableMemory(leaks, limit, &num_leaks, &leak_bytes);
337
338 ok = ok && pipe.Sender().Send(num_allocations);
339 ok = ok && pipe.Sender().Send(allocation_bytes);
340 ok = ok && pipe.Sender().Send(num_leaks);
341 ok = ok && pipe.Sender().Send(leak_bytes);
342 ok = ok && pipe.Sender().SendVector(leaks);
343
344 if (!ok) {
345 _exit(3);
346 }
347
348 _exit(0);
349 } else {
350 // Nothing left to do in the collection thread, return immediately,
351 // releasing all the captured threads.
Christopher Ferris47dea712017-05-03 17:34:29 -0700352 MEM_ALOGI("collection thread done");
Colin Cross7add50d2016-01-14 15:35:40 -0800353 return 0;
354 }
355 }};
356
357 /////////////////////////////////////////////
358 // Original thread
359 /////////////////////////////////////////////
360
361 {
362 // Disable malloc to get a consistent view of memory
363 ScopedDisableMalloc disable_malloc;
364
365 // Start the collection thread
366 thread.Start();
367
368 // Wait for the collection thread to signal that it is ready to fork the
369 // heap walker process.
Colin Crossde42af02016-01-14 15:35:40 -0800370 continue_parent_sem.Wait(30s);
Colin Cross7add50d2016-01-14 15:35:40 -0800371
372 // Re-enable malloc so the collection thread can fork.
373 }
374
375 // Wait for the collection thread to exit
376 int ret = thread.Join();
377 if (ret != 0) {
378 return false;
379 }
380
381 // Get a pipe from the heap walker process. Transferring a new pipe fd
382 // ensures no other forked processes can have it open, so when the heap
383 // walker process dies the remote side of the pipe will close.
384 if (!pipe.OpenReceiver()) {
385 return false;
386 }
387
388 bool ok = true;
389 ok = ok && pipe.Receiver().Receive(&info.num_allocations);
390 ok = ok && pipe.Receiver().Receive(&info.allocation_bytes);
391 ok = ok && pipe.Receiver().Receive(&info.num_leaks);
392 ok = ok && pipe.Receiver().Receive(&info.leak_bytes);
393 ok = ok && pipe.Receiver().ReceiveVector(info.leaks);
394 if (!ok) {
395 return false;
396 }
397
Christopher Ferris47dea712017-05-03 17:34:29 -0700398 MEM_ALOGI("unreachable memory detection done");
399 MEM_ALOGE("%zu bytes in %zu allocation%s unreachable out of %zu bytes in %zu allocation%s",
400 info.leak_bytes, info.num_leaks, plural(info.num_leaks), info.allocation_bytes,
401 info.num_allocations, plural(info.num_allocations));
Colin Cross7add50d2016-01-14 15:35:40 -0800402 return true;
403}
404
405std::string Leak::ToString(bool log_contents) const {
Colin Cross7add50d2016-01-14 15:35:40 -0800406 std::ostringstream oss;
407
408 oss << " " << std::dec << size;
Colin Crossde42af02016-01-14 15:35:40 -0800409 oss << " bytes unreachable at ";
Colin Cross7add50d2016-01-14 15:35:40 -0800410 oss << std::hex << begin;
411 oss << std::endl;
Colin Cross7a22e812016-03-04 16:36:12 -0800412 if (referenced_count > 0) {
413 oss << std::dec;
414 oss << " referencing " << referenced_size << " unreachable bytes";
415 oss << " in " << referenced_count << " allocation" << plural(referenced_count);
416 oss << std::endl;
417 }
418 if (similar_count > 0) {
419 oss << std::dec;
420 oss << " and " << similar_size << " similar unreachable bytes";
421 oss << " in " << similar_count << " allocation" << plural(similar_count);
422 oss << std::endl;
423 if (similar_referenced_count > 0) {
424 oss << " referencing " << similar_referenced_size << " unreachable bytes";
425 oss << " in " << similar_referenced_count << " allocation" << plural(similar_referenced_count);
426 oss << std::endl;
427 }
428 }
Colin Cross7add50d2016-01-14 15:35:40 -0800429
430 if (log_contents) {
431 const int bytes_per_line = 16;
432 const size_t bytes = std::min(size, contents_length);
433
434 if (bytes == size) {
435 oss << " contents:" << std::endl;
436 } else {
Colin Cross7a22e812016-03-04 16:36:12 -0800437 oss << " first " << bytes << " bytes of contents:" << std::endl;
Colin Cross7add50d2016-01-14 15:35:40 -0800438 }
439
440 for (size_t i = 0; i < bytes; i += bytes_per_line) {
441 oss << " " << std::hex << begin + i << ": ";
442 size_t j;
443 oss << std::setfill('0');
444 for (j = i; j < bytes && j < i + bytes_per_line; j++) {
445 oss << std::setw(2) << static_cast<int>(contents[j]) << " ";
446 }
447 oss << std::setfill(' ');
448 for (; j < i + bytes_per_line; j++) {
449 oss << " ";
450 }
451 for (j = i; j < bytes && j < i + bytes_per_line; j++) {
452 char c = contents[j];
453 if (c < ' ' || c >= 0x7f) {
454 c = '.';
455 }
456 oss << c;
457 }
458 oss << std::endl;
459 }
460 }
Colin Cross7a22e812016-03-04 16:36:12 -0800461 if (backtrace.num_frames > 0) {
462 oss << backtrace_string(backtrace.frames, backtrace.num_frames);
Colin Cross7add50d2016-01-14 15:35:40 -0800463 }
464
465 return oss.str();
466}
467
Colin Cross11185af2016-03-04 16:37:02 -0800468// Figure out the abi based on defined macros.
469#if defined(__arm__)
470#define ABI_STRING "arm"
471#elif defined(__aarch64__)
472#define ABI_STRING "arm64"
473#elif defined(__mips__) && !defined(__LP64__)
474#define ABI_STRING "mips"
475#elif defined(__mips__) && defined(__LP64__)
476#define ABI_STRING "mips64"
477#elif defined(__i386__)
478#define ABI_STRING "x86"
479#elif defined(__x86_64__)
480#define ABI_STRING "x86_64"
481#else
482#error "Unsupported ABI"
483#endif
484
Colin Cross7add50d2016-01-14 15:35:40 -0800485std::string UnreachableMemoryInfo::ToString(bool log_contents) const {
486 std::ostringstream oss;
487 oss << " " << leak_bytes << " bytes in ";
Colin Cross7a22e812016-03-04 16:36:12 -0800488 oss << num_leaks << " unreachable allocation" << plural(num_leaks);
Colin Cross7add50d2016-01-14 15:35:40 -0800489 oss << std::endl;
Colin Cross11185af2016-03-04 16:37:02 -0800490 oss << " ABI: '" ABI_STRING "'" << std::endl;
491 oss << std::endl;
Colin Cross7add50d2016-01-14 15:35:40 -0800492
493 for (auto it = leaks.begin(); it != leaks.end(); it++) {
Colin Crossa83881e2017-06-22 10:50:05 -0700494 oss << it->ToString(log_contents);
495 oss << std::endl;
Colin Cross7add50d2016-01-14 15:35:40 -0800496 }
497
498 return oss.str();
499}
500
501std::string GetUnreachableMemoryString(bool log_contents, size_t limit) {
502 UnreachableMemoryInfo info;
503 if (!GetUnreachableMemory(info, limit)) {
Colin Cross72d38812017-06-13 16:41:58 -0700504 return "Failed to get unreachable memory\n"
505 "If you are trying to get unreachable memory from a system app\n"
506 "(like com.android.systemui), disable selinux first using\n"
507 "setenforce 0\n";
Colin Cross7add50d2016-01-14 15:35:40 -0800508 }
509
510 return info.ToString(log_contents);
511}
512
513bool LogUnreachableMemory(bool log_contents, size_t limit) {
514 UnreachableMemoryInfo info;
515 if (!GetUnreachableMemory(info, limit)) {
516 return false;
517 }
518
519 for (auto it = info.leaks.begin(); it != info.leaks.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700520 MEM_ALOGE("%s", it->ToString(log_contents).c_str());
Colin Cross7add50d2016-01-14 15:35:40 -0800521 }
522 return true;
523}
524
Colin Cross7add50d2016-01-14 15:35:40 -0800525bool NoLeaks() {
526 UnreachableMemoryInfo info;
527 if (!GetUnreachableMemory(info, 0)) {
528 return false;
529 }
530
531 return info.num_leaks == 0;
532}