1/*
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#ifndef LIBMEMUNREACHABLE_MEMUNREACHABLE_H_
18#define LIBMEMUNREACHABLE_MEMUNREACHABLE_H_
19
20#include <sys/cdefs.h>
21
22#ifdef __cplusplus
23
24#include <vector>
25#include <string>
26
27struct Leak {
28  uintptr_t begin;
29  size_t size;
30
31  size_t referenced_count;
32  size_t referenced_size;
33
34  size_t similar_count;
35  size_t similar_size;
36  size_t similar_referenced_count;
37  size_t similar_referenced_size;
38
39  size_t total_size;
40
41  static const size_t contents_length = 32;
42  char contents[contents_length];
43
44  struct Backtrace {
45    size_t num_frames;
46
47    static const size_t max_frames = 16;
48    uintptr_t frames[max_frames];
49  } backtrace;
50
51  std::string ToString(bool log_contents) const;
52};
53
54struct UnreachableMemoryInfo {
55  std::vector<Leak> leaks;
56  size_t num_leaks;
57  size_t leak_bytes;
58  size_t num_allocations;
59  size_t allocation_bytes;
60
61  UnreachableMemoryInfo() {}
62  ~UnreachableMemoryInfo() {
63    // Clear the memory that holds the leaks, otherwise the next attempt to
64    // detect leaks may find the old data (for example in the jemalloc tcache)
65    // and consider all the leaks to be referenced.
66    memset(leaks.data(), 0, leaks.capacity() * sizeof(Leak));
67  }
68
69  std::string ToString(bool log_contents) const;
70};
71
72bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit = 100);
73
74std::string GetUnreachableMemoryString(bool log_contents = false, size_t limit = 100);
75
76#endif
77
78__BEGIN_DECLS
79
80bool LogUnreachableMemory(bool log_contents, size_t limit);
81
82bool NoLeaks();
83
84__END_DECLS
85
86#endif // LIBMEMUNREACHABLE_MEMUNREACHABLE_H_
87