1/*
2 * Copyright 2017, 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#define DEBUG false  // STOPSHIP if true
18#include "Log.h"
19
20#include <sstream>
21#include "MemoryLeakTrackUtil.h"
22
23/*
24 * The code here originally resided in MediaPlayerService.cpp
25 */
26
27// Figure out the abi based on defined macros.
28#if defined(__arm__)
29#define ABI_STRING "arm"
30#elif defined(__aarch64__)
31#define ABI_STRING "arm64"
32#elif defined(__mips__) && !defined(__LP64__)
33#define ABI_STRING "mips"
34#elif defined(__mips__) && defined(__LP64__)
35#define ABI_STRING "mips64"
36#elif defined(__i386__)
37#define ABI_STRING "x86"
38#elif defined(__x86_64__)
39#define ABI_STRING "x86_64"
40#else
41#error "Unsupported ABI"
42#endif
43
44extern std::string backtrace_string(const uintptr_t* frames, size_t frame_count);
45
46namespace android {
47namespace os {
48namespace statsd {
49
50extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize, size_t* infoSize,
51                                     size_t* totalMemory, size_t* backtraceSize);
52
53extern "C" void free_malloc_leak_info(uint8_t* info);
54
55std::string dumpMemInfo(size_t limit) {
56    uint8_t* info;
57    size_t overallSize;
58    size_t infoSize;
59    size_t totalMemory;
60    size_t backtraceSize;
61    get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize);
62
63    size_t count;
64    if (info == nullptr || overallSize == 0 || infoSize == 0 ||
65        (count = overallSize / infoSize) == 0) {
66        VLOG("no malloc info, libc.debug.malloc.program property should be set");
67        return std::string();
68    }
69
70    std::ostringstream oss;
71    oss << totalMemory << " bytes in " << count << " allocations\n";
72    oss << "  ABI: '" ABI_STRING "'"
73        << "\n\n";
74    if (count > limit) count = limit;
75
76    // The memory is sorted based on total size which is useful for finding
77    // worst memory offenders. For diffs, sometimes it is preferable to sort
78    // based on the backtrace.
79    for (size_t i = 0; i < count; i++) {
80        struct AllocEntry {
81            size_t size;  // bit 31 is set if this is zygote allocated memory
82            size_t allocations;
83            uintptr_t backtrace[];
84        };
85
86        const AllocEntry* const e = (AllocEntry*)(info + i * infoSize);
87
88        oss << (e->size * e->allocations) << " bytes ( " << e->size << " bytes * " << e->allocations
89            << " allocations )\n";
90        oss << backtrace_string(e->backtrace, backtraceSize) << "\n";
91    }
92    oss << "\n";
93    free_malloc_leak_info(info);
94    return oss.str();
95}
96
97}  // namespace statsd
98}  // namespace os
99}  // namespace android