blob: a1f74c333fe0de284328945429f35ef4d4dd70f2 [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>
Colin Crossa9939e92017-06-21 13:13:00 -070018#include <string.h>
Colin Cross7add50d2016-01-14 15:35:40 -080019
20#include <functional>
21#include <iomanip>
22#include <mutex>
Colin Cross7add50d2016-01-14 15:35:40 -080023#include <sstream>
Colin Crossa83881e2017-06-22 10:50:05 -070024#include <string>
Colin Cross7a22e812016-03-04 16:36:12 -080025#include <unordered_map>
Colin Cross7add50d2016-01-14 15:35:40 -080026
Colin Cross7add50d2016-01-14 15:35:40 -080027#include <android-base/macros.h>
Colin Crossa83881e2017-06-22 10:50:05 -070028#include <backtrace.h>
Colin Cross7add50d2016-01-14 15:35:40 -080029
30#include "Allocator.h"
31#include "HeapWalker.h"
Colin Cross7a22e812016-03-04 16:36:12 -080032#include "Leak.h"
Colin Cross8e8f34c2016-03-02 17:53:39 -080033#include "LeakFolding.h"
Colin Cross7add50d2016-01-14 15:35:40 -080034#include "LeakPipe.h"
35#include "ProcessMappings.h"
36#include "PtracerThread.h"
37#include "ScopedDisableMalloc.h"
38#include "Semaphore.h"
39#include "ThreadCapture.h"
40
Colin Cross7add50d2016-01-14 15:35:40 -080041#include "bionic.h"
42#include "log.h"
Colin Crossa83881e2017-06-22 10:50:05 -070043#include "memunreachable/memunreachable.h"
Colin Cross7add50d2016-01-14 15:35:40 -080044
Colin Cross7add50d2016-01-14 15:35:40 -080045using namespace std::chrono_literals;
46
Colin Crossa9939e92017-06-21 13:13:00 -070047namespace android {
48
49const size_t Leak::contents_length;
50
Colin Cross7add50d2016-01-14 15:35:40 -080051class MemUnreachable {
52 public:
Colin Crossa83881e2017-06-22 10:50:05 -070053 MemUnreachable(pid_t pid, Allocator<void> allocator)
54 : pid_(pid), allocator_(allocator), heap_walker_(allocator_) {}
Colin Cross7add50d2016-01-14 15:35:40 -080055 bool CollectAllocations(const allocator::vector<ThreadInfo>& threads,
Colin Crossa83881e2017-06-22 10:50:05 -070056 const allocator::vector<Mapping>& mappings);
57 bool GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit, size_t* num_leaks,
58 size_t* leak_bytes);
Colin Cross7add50d2016-01-14 15:35:40 -080059 size_t Allocations() { return heap_walker_.Allocations(); }
60 size_t AllocationBytes() { return heap_walker_.AllocationBytes(); }
Colin Crossa83881e2017-06-22 10:50:05 -070061
Colin Cross7add50d2016-01-14 15:35:40 -080062 private:
63 bool ClassifyMappings(const allocator::vector<Mapping>& mappings,
Colin Crossa83881e2017-06-22 10:50:05 -070064 allocator::vector<Mapping>& heap_mappings,
65 allocator::vector<Mapping>& anon_mappings,
66 allocator::vector<Mapping>& globals_mappings,
67 allocator::vector<Mapping>& stack_mappings);
Colin Cross7add50d2016-01-14 15:35:40 -080068 DISALLOW_COPY_AND_ASSIGN(MemUnreachable);
69 pid_t pid_;
70 Allocator<void> allocator_;
71 HeapWalker heap_walker_;
72};
73
74static void HeapIterate(const Mapping& heap_mapping,
Colin Crossa83881e2017-06-22 10:50:05 -070075 const std::function<void(uintptr_t, size_t)>& func) {
Colin Cross7add50d2016-01-14 15:35:40 -080076 malloc_iterate(heap_mapping.begin, heap_mapping.end - heap_mapping.begin,
Colin Crossa83881e2017-06-22 10:50:05 -070077 [](uintptr_t base, size_t size, void* arg) {
78 auto f = reinterpret_cast<const std::function<void(uintptr_t, size_t)>*>(arg);
79 (*f)(base, size);
80 },
81 const_cast<void*>(reinterpret_cast<const void*>(&func)));
Colin Cross7add50d2016-01-14 15:35:40 -080082}
83
84bool MemUnreachable::CollectAllocations(const allocator::vector<ThreadInfo>& threads,
Colin Crossa83881e2017-06-22 10:50:05 -070085 const allocator::vector<Mapping>& mappings) {
Christopher Ferris47dea712017-05-03 17:34:29 -070086 MEM_ALOGI("searching process %d for allocations", pid_);
Colin Cross7add50d2016-01-14 15:35:40 -080087 allocator::vector<Mapping> heap_mappings{mappings};
88 allocator::vector<Mapping> anon_mappings{mappings};
89 allocator::vector<Mapping> globals_mappings{mappings};
90 allocator::vector<Mapping> stack_mappings{mappings};
Christopher Ferris47dea712017-05-03 17:34:29 -070091 if (!ClassifyMappings(mappings, heap_mappings, anon_mappings, globals_mappings, stack_mappings)) {
Colin Cross7add50d2016-01-14 15:35:40 -080092 return false;
93 }
94
95 for (auto it = heap_mappings.begin(); it != heap_mappings.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -070096 MEM_ALOGV("Heap mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
97 HeapIterate(*it,
98 [&](uintptr_t base, size_t size) { heap_walker_.Allocation(base, base + size); });
Colin Cross7add50d2016-01-14 15:35:40 -080099 }
100
101 for (auto it = anon_mappings.begin(); it != anon_mappings.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700102 MEM_ALOGV("Anon mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
Colin Cross7add50d2016-01-14 15:35:40 -0800103 heap_walker_.Allocation(it->begin, it->end);
104 }
105
106 for (auto it = globals_mappings.begin(); it != globals_mappings.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700107 MEM_ALOGV("Globals mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
Colin Cross7add50d2016-01-14 15:35:40 -0800108 heap_walker_.Root(it->begin, it->end);
109 }
110
111 for (auto thread_it = threads.begin(); thread_it != threads.end(); thread_it++) {
112 for (auto it = stack_mappings.begin(); it != stack_mappings.end(); it++) {
113 if (thread_it->stack.first >= it->begin && thread_it->stack.first <= it->end) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700114 MEM_ALOGV("Stack %" PRIxPTR "-%" PRIxPTR " %s", thread_it->stack.first, it->end, it->name);
Colin Cross7add50d2016-01-14 15:35:40 -0800115 heap_walker_.Root(thread_it->stack.first, it->end);
116 }
117 }
118 heap_walker_.Root(thread_it->regs);
119 }
120
Christopher Ferris47dea712017-05-03 17:34:29 -0700121 MEM_ALOGI("searching done");
Colin Cross7add50d2016-01-14 15:35:40 -0800122
123 return true;
124}
125
Colin Crossa83881e2017-06-22 10:50:05 -0700126bool MemUnreachable::GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit,
127 size_t* num_leaks, size_t* leak_bytes) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700128 MEM_ALOGI("sweeping process %d for unreachable memory", pid_);
Colin Cross7add50d2016-01-14 15:35:40 -0800129 leaks.clear();
130
Colin Cross8e8f34c2016-03-02 17:53:39 -0800131 if (!heap_walker_.DetectLeaks()) {
132 return false;
133 }
134
Colin Cross7a22e812016-03-04 16:36:12 -0800135 allocator::vector<Range> leaked1{allocator_};
136 heap_walker_.Leaked(leaked1, 0, num_leaks, leak_bytes);
137
Christopher Ferris47dea712017-05-03 17:34:29 -0700138 MEM_ALOGI("sweeping done");
Colin Cross7a22e812016-03-04 16:36:12 -0800139
Christopher Ferris47dea712017-05-03 17:34:29 -0700140 MEM_ALOGI("folding related leaks");
Colin Cross7a22e812016-03-04 16:36:12 -0800141
Colin Cross8e8f34c2016-03-02 17:53:39 -0800142 LeakFolding folding(allocator_, heap_walker_);
143 if (!folding.FoldLeaks()) {
144 return false;
145 }
146
147 allocator::vector<LeakFolding::Leak> leaked{allocator_};
148
Colin Cross7a22e812016-03-04 16:36:12 -0800149 if (!folding.Leaked(leaked, num_leaks, leak_bytes)) {
Colin Cross7add50d2016-01-14 15:35:40 -0800150 return false;
151 }
152
Colin Cross7a22e812016-03-04 16:36:12 -0800153 allocator::unordered_map<Leak::Backtrace, Leak*> backtrace_map{allocator_};
154
155 // Prevent reallocations of backing memory so we can store pointers into it
156 // in backtrace_map.
157 leaks.reserve(leaked.size());
158
Colin Crossa83881e2017-06-22 10:50:05 -0700159 for (auto& it : leaked) {
Colin Cross7a22e812016-03-04 16:36:12 -0800160 leaks.emplace_back();
161 Leak* leak = &leaks.back();
162
Colin Crossa83881e2017-06-22 10:50:05 -0700163 ssize_t num_backtrace_frames = malloc_backtrace(
164 reinterpret_cast<void*>(it.range.begin), leak->backtrace.frames, leak->backtrace.max_frames);
Colin Cross7add50d2016-01-14 15:35:40 -0800165 if (num_backtrace_frames > 0) {
Colin Cross7a22e812016-03-04 16:36:12 -0800166 leak->backtrace.num_frames = num_backtrace_frames;
167
168 auto inserted = backtrace_map.emplace(leak->backtrace, leak);
169 if (!inserted.second) {
170 // Leak with same backtrace already exists, drop this one and
171 // increment similar counts on the existing one.
172 leaks.pop_back();
173 Leak* similar_leak = inserted.first->second;
174 similar_leak->similar_count++;
175 similar_leak->similar_size += it.range.size();
176 similar_leak->similar_referenced_count += it.referenced_count;
177 similar_leak->similar_referenced_size += it.referenced_size;
178 similar_leak->total_size += it.range.size();
179 similar_leak->total_size += it.referenced_size;
180 continue;
181 }
Colin Cross7add50d2016-01-14 15:35:40 -0800182 }
Colin Cross7a22e812016-03-04 16:36:12 -0800183
184 leak->begin = it.range.begin;
185 leak->size = it.range.size();
186 leak->referenced_count = it.referenced_count;
187 leak->referenced_size = it.referenced_size;
188 leak->total_size = leak->size + leak->referenced_size;
189 memcpy(leak->contents, reinterpret_cast<void*>(it.range.begin),
Colin Crossa83881e2017-06-22 10:50:05 -0700190 std::min(leak->size, Leak::contents_length));
Colin Cross7add50d2016-01-14 15:35:40 -0800191 }
192
Christopher Ferris47dea712017-05-03 17:34:29 -0700193 MEM_ALOGI("folding done");
Colin Cross7a22e812016-03-04 16:36:12 -0800194
Colin Crossa83881e2017-06-22 10:50:05 -0700195 std::sort(leaks.begin(), leaks.end(),
196 [](const Leak& a, const Leak& b) { return a.total_size > b.total_size; });
Colin Cross7a22e812016-03-04 16:36:12 -0800197
198 if (leaks.size() > limit) {
199 leaks.resize(limit);
200 }
Colin Cross7add50d2016-01-14 15:35:40 -0800201
202 return true;
203}
204
205static bool has_prefix(const allocator::string& s, const char* prefix) {
206 int ret = s.compare(0, strlen(prefix), prefix);
207 return ret == 0;
208}
209
210bool MemUnreachable::ClassifyMappings(const allocator::vector<Mapping>& mappings,
Colin Crossa83881e2017-06-22 10:50:05 -0700211 allocator::vector<Mapping>& heap_mappings,
212 allocator::vector<Mapping>& anon_mappings,
213 allocator::vector<Mapping>& globals_mappings,
214 allocator::vector<Mapping>& stack_mappings) {
Colin Cross7add50d2016-01-14 15:35:40 -0800215 heap_mappings.clear();
216 anon_mappings.clear();
217 globals_mappings.clear();
218 stack_mappings.clear();
219
220 allocator::string current_lib{allocator_};
221
222 for (auto it = mappings.begin(); it != mappings.end(); it++) {
223 if (it->execute) {
224 current_lib = it->name;
225 continue;
226 }
227
228 if (!it->read) {
229 continue;
230 }
231
232 const allocator::string mapping_name{it->name, allocator_};
233 if (mapping_name == "[anon:.bss]") {
234 // named .bss section
235 globals_mappings.emplace_back(*it);
236 } else if (mapping_name == current_lib) {
237 // .rodata or .data section
238 globals_mappings.emplace_back(*it);
239 } else if (mapping_name == "[anon:libc_malloc]") {
240 // named malloc mapping
241 heap_mappings.emplace_back(*it);
242 } else if (has_prefix(mapping_name, "/dev/ashmem/dalvik")) {
243 // named dalvik heap mapping
244 globals_mappings.emplace_back(*it);
245 } else if (has_prefix(mapping_name, "[stack")) {
246 // named stack mapping
247 stack_mappings.emplace_back(*it);
248 } else if (mapping_name.size() == 0) {
249 globals_mappings.emplace_back(*it);
Colin Crossa83881e2017-06-22 10:50:05 -0700250 } else if (has_prefix(mapping_name, "[anon:") &&
251 mapping_name != "[anon:leak_detector_malloc]") {
Colin Cross7add50d2016-01-14 15:35:40 -0800252 // TODO(ccross): it would be nice to treat named anonymous mappings as
253 // possible leaks, but naming something in a .bss or .data section makes
254 // it impossible to distinguish them from mmaped and then named mappings.
255 globals_mappings.emplace_back(*it);
256 }
257 }
258
259 return true;
260}
261
Colin Crossa83881e2017-06-22 10:50:05 -0700262template <typename T>
Colin Cross7a22e812016-03-04 16:36:12 -0800263static inline const char* plural(T val) {
264 return (val == 1) ? "" : "s";
265}
266
Colin Cross7add50d2016-01-14 15:35:40 -0800267bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit) {
268 int parent_pid = getpid();
269 int parent_tid = gettid();
270
271 Heap heap;
272
273 Semaphore continue_parent_sem;
274 LeakPipe pipe;
275
276 PtracerThread thread{[&]() -> int {
277 /////////////////////////////////////////////
278 // Collection thread
279 /////////////////////////////////////////////
Christopher Ferris47dea712017-05-03 17:34:29 -0700280 MEM_ALOGI("collecting thread info for process %d...", parent_pid);
Colin Cross7add50d2016-01-14 15:35:40 -0800281
282 ThreadCapture thread_capture(parent_pid, heap);
283 allocator::vector<ThreadInfo> thread_info(heap);
284 allocator::vector<Mapping> mappings(heap);
285
286 // ptrace all the threads
287 if (!thread_capture.CaptureThreads()) {
Colin Crossde42af02016-01-14 15:35:40 -0800288 continue_parent_sem.Post();
Colin Cross7add50d2016-01-14 15:35:40 -0800289 return 1;
290 }
291
292 // collect register contents and stacks
293 if (!thread_capture.CapturedThreadInfo(thread_info)) {
Colin Crossde42af02016-01-14 15:35:40 -0800294 continue_parent_sem.Post();
Colin Cross7add50d2016-01-14 15:35:40 -0800295 return 1;
296 }
297
298 // snapshot /proc/pid/maps
299 if (!ProcessMappings(parent_pid, mappings)) {
Colin Crossde42af02016-01-14 15:35:40 -0800300 continue_parent_sem.Post();
Colin Cross7add50d2016-01-14 15:35:40 -0800301 return 1;
302 }
303
304 // malloc must be enabled to call fork, at_fork handlers take the same
305 // locks as ScopedDisableMalloc. All threads are paused in ptrace, so
306 // memory state is still consistent. Unfreeze the original thread so it
307 // can drop the malloc locks, it will block until the collection thread
308 // exits.
309 thread_capture.ReleaseThread(parent_tid);
310 continue_parent_sem.Post();
311
312 // fork a process to do the heap walking
313 int ret = fork();
314 if (ret < 0) {
315 return 1;
316 } else if (ret == 0) {
317 /////////////////////////////////////////////
318 // Heap walker process
319 /////////////////////////////////////////////
320 // Examine memory state in the child using the data collected above and
321 // the CoW snapshot of the process memory contents.
322
323 if (!pipe.OpenSender()) {
324 _exit(1);
325 }
326
327 MemUnreachable unreachable{parent_pid, heap};
328
329 if (!unreachable.CollectAllocations(thread_info, mappings)) {
330 _exit(2);
331 }
332 size_t num_allocations = unreachable.Allocations();
333 size_t allocation_bytes = unreachable.AllocationBytes();
334
335 allocator::vector<Leak> leaks{heap};
336
337 size_t num_leaks = 0;
338 size_t leak_bytes = 0;
339 bool ok = unreachable.GetUnreachableMemory(leaks, limit, &num_leaks, &leak_bytes);
340
341 ok = ok && pipe.Sender().Send(num_allocations);
342 ok = ok && pipe.Sender().Send(allocation_bytes);
343 ok = ok && pipe.Sender().Send(num_leaks);
344 ok = ok && pipe.Sender().Send(leak_bytes);
345 ok = ok && pipe.Sender().SendVector(leaks);
346
347 if (!ok) {
348 _exit(3);
349 }
350
351 _exit(0);
352 } else {
353 // Nothing left to do in the collection thread, return immediately,
354 // releasing all the captured threads.
Christopher Ferris47dea712017-05-03 17:34:29 -0700355 MEM_ALOGI("collection thread done");
Colin Cross7add50d2016-01-14 15:35:40 -0800356 return 0;
357 }
358 }};
359
360 /////////////////////////////////////////////
361 // Original thread
362 /////////////////////////////////////////////
363
364 {
365 // Disable malloc to get a consistent view of memory
366 ScopedDisableMalloc disable_malloc;
367
368 // Start the collection thread
369 thread.Start();
370
371 // Wait for the collection thread to signal that it is ready to fork the
372 // heap walker process.
Colin Crossde42af02016-01-14 15:35:40 -0800373 continue_parent_sem.Wait(30s);
Colin Cross7add50d2016-01-14 15:35:40 -0800374
375 // Re-enable malloc so the collection thread can fork.
376 }
377
378 // Wait for the collection thread to exit
379 int ret = thread.Join();
380 if (ret != 0) {
381 return false;
382 }
383
384 // Get a pipe from the heap walker process. Transferring a new pipe fd
385 // ensures no other forked processes can have it open, so when the heap
386 // walker process dies the remote side of the pipe will close.
387 if (!pipe.OpenReceiver()) {
388 return false;
389 }
390
391 bool ok = true;
392 ok = ok && pipe.Receiver().Receive(&info.num_allocations);
393 ok = ok && pipe.Receiver().Receive(&info.allocation_bytes);
394 ok = ok && pipe.Receiver().Receive(&info.num_leaks);
395 ok = ok && pipe.Receiver().Receive(&info.leak_bytes);
396 ok = ok && pipe.Receiver().ReceiveVector(info.leaks);
397 if (!ok) {
398 return false;
399 }
400
Christopher Ferris47dea712017-05-03 17:34:29 -0700401 MEM_ALOGI("unreachable memory detection done");
402 MEM_ALOGE("%zu bytes in %zu allocation%s unreachable out of %zu bytes in %zu allocation%s",
403 info.leak_bytes, info.num_leaks, plural(info.num_leaks), info.allocation_bytes,
404 info.num_allocations, plural(info.num_allocations));
Colin Cross7add50d2016-01-14 15:35:40 -0800405 return true;
406}
407
408std::string Leak::ToString(bool log_contents) const {
Colin Cross7add50d2016-01-14 15:35:40 -0800409 std::ostringstream oss;
410
411 oss << " " << std::dec << size;
Colin Crossde42af02016-01-14 15:35:40 -0800412 oss << " bytes unreachable at ";
Colin Cross7add50d2016-01-14 15:35:40 -0800413 oss << std::hex << begin;
414 oss << std::endl;
Colin Cross7a22e812016-03-04 16:36:12 -0800415 if (referenced_count > 0) {
416 oss << std::dec;
417 oss << " referencing " << referenced_size << " unreachable bytes";
418 oss << " in " << referenced_count << " allocation" << plural(referenced_count);
419 oss << std::endl;
420 }
421 if (similar_count > 0) {
422 oss << std::dec;
423 oss << " and " << similar_size << " similar unreachable bytes";
424 oss << " in " << similar_count << " allocation" << plural(similar_count);
425 oss << std::endl;
426 if (similar_referenced_count > 0) {
427 oss << " referencing " << similar_referenced_size << " unreachable bytes";
428 oss << " in " << similar_referenced_count << " allocation" << plural(similar_referenced_count);
429 oss << std::endl;
430 }
431 }
Colin Cross7add50d2016-01-14 15:35:40 -0800432
433 if (log_contents) {
434 const int bytes_per_line = 16;
435 const size_t bytes = std::min(size, contents_length);
436
437 if (bytes == size) {
438 oss << " contents:" << std::endl;
439 } else {
Colin Cross7a22e812016-03-04 16:36:12 -0800440 oss << " first " << bytes << " bytes of contents:" << std::endl;
Colin Cross7add50d2016-01-14 15:35:40 -0800441 }
442
443 for (size_t i = 0; i < bytes; i += bytes_per_line) {
444 oss << " " << std::hex << begin + i << ": ";
445 size_t j;
446 oss << std::setfill('0');
447 for (j = i; j < bytes && j < i + bytes_per_line; j++) {
448 oss << std::setw(2) << static_cast<int>(contents[j]) << " ";
449 }
450 oss << std::setfill(' ');
451 for (; j < i + bytes_per_line; j++) {
452 oss << " ";
453 }
454 for (j = i; j < bytes && j < i + bytes_per_line; j++) {
455 char c = contents[j];
456 if (c < ' ' || c >= 0x7f) {
457 c = '.';
458 }
459 oss << c;
460 }
461 oss << std::endl;
462 }
463 }
Colin Cross7a22e812016-03-04 16:36:12 -0800464 if (backtrace.num_frames > 0) {
465 oss << backtrace_string(backtrace.frames, backtrace.num_frames);
Colin Cross7add50d2016-01-14 15:35:40 -0800466 }
467
468 return oss.str();
469}
470
Colin Cross11185af2016-03-04 16:37:02 -0800471// Figure out the abi based on defined macros.
472#if defined(__arm__)
473#define ABI_STRING "arm"
474#elif defined(__aarch64__)
475#define ABI_STRING "arm64"
476#elif defined(__mips__) && !defined(__LP64__)
477#define ABI_STRING "mips"
478#elif defined(__mips__) && defined(__LP64__)
479#define ABI_STRING "mips64"
480#elif defined(__i386__)
481#define ABI_STRING "x86"
482#elif defined(__x86_64__)
483#define ABI_STRING "x86_64"
484#else
485#error "Unsupported ABI"
486#endif
487
Colin Cross7add50d2016-01-14 15:35:40 -0800488std::string UnreachableMemoryInfo::ToString(bool log_contents) const {
489 std::ostringstream oss;
490 oss << " " << leak_bytes << " bytes in ";
Colin Cross7a22e812016-03-04 16:36:12 -0800491 oss << num_leaks << " unreachable allocation" << plural(num_leaks);
Colin Cross7add50d2016-01-14 15:35:40 -0800492 oss << std::endl;
Colin Cross11185af2016-03-04 16:37:02 -0800493 oss << " ABI: '" ABI_STRING "'" << std::endl;
494 oss << std::endl;
Colin Cross7add50d2016-01-14 15:35:40 -0800495
496 for (auto it = leaks.begin(); it != leaks.end(); it++) {
Colin Crossa83881e2017-06-22 10:50:05 -0700497 oss << it->ToString(log_contents);
498 oss << std::endl;
Colin Cross7add50d2016-01-14 15:35:40 -0800499 }
500
501 return oss.str();
502}
503
504std::string GetUnreachableMemoryString(bool log_contents, size_t limit) {
505 UnreachableMemoryInfo info;
506 if (!GetUnreachableMemory(info, limit)) {
Colin Cross72d38812017-06-13 16:41:58 -0700507 return "Failed to get unreachable memory\n"
508 "If you are trying to get unreachable memory from a system app\n"
509 "(like com.android.systemui), disable selinux first using\n"
510 "setenforce 0\n";
Colin Cross7add50d2016-01-14 15:35:40 -0800511 }
512
513 return info.ToString(log_contents);
514}
515
Colin Crossa9939e92017-06-21 13:13:00 -0700516} // namespace android
517
Colin Cross7add50d2016-01-14 15:35:40 -0800518bool LogUnreachableMemory(bool log_contents, size_t limit) {
Colin Crossa9939e92017-06-21 13:13:00 -0700519 android::UnreachableMemoryInfo info;
520 if (!android::GetUnreachableMemory(info, limit)) {
Colin Cross7add50d2016-01-14 15:35:40 -0800521 return false;
522 }
523
524 for (auto it = info.leaks.begin(); it != info.leaks.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700525 MEM_ALOGE("%s", it->ToString(log_contents).c_str());
Colin Cross7add50d2016-01-14 15:35:40 -0800526 }
527 return true;
528}
529
Colin Cross7add50d2016-01-14 15:35:40 -0800530bool NoLeaks() {
Colin Crossa9939e92017-06-21 13:13:00 -0700531 android::UnreachableMemoryInfo info;
532 if (!android::GetUnreachableMemory(info, 0)) {
Colin Cross7add50d2016-01-14 15:35:40 -0800533 return false;
534 }
535
536 return info.num_leaks == 0;
537}