1/*
2 * Copyright 2011, 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
18//#define LOG_NDEBUG 0
19#define LOG_TAG "MemoryLeackTrackUtil"
20#include <utils/Log.h>
21
22#include "media/MemoryLeakTrackUtil.h"
23#include <sstream>
24
25/*
26 * The code here originally resided in MediaPlayerService.cpp
27 */
28
29// Figure out the abi based on defined macros.
30#if defined(__arm__)
31#define ABI_STRING "arm"
32#elif defined(__aarch64__)
33#define ABI_STRING "arm64"
34#elif defined(__mips__) && !defined(__LP64__)
35#define ABI_STRING "mips"
36#elif defined(__mips__) && defined(__LP64__)
37#define ABI_STRING "mips64"
38#elif defined(__i386__)
39#define ABI_STRING "x86"
40#elif defined(__x86_64__)
41#define ABI_STRING "x86_64"
42#else
43#error "Unsupported ABI"
44#endif
45
46extern std::string backtrace_string(const uintptr_t* frames, size_t frame_count);
47
48namespace android {
49
50extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
51        size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
52
53extern "C" void free_malloc_leak_info(uint8_t* info);
54
55std::string dumpMemoryAddresses(size_t limit)
56{
57    uint8_t *info;
58    size_t overallSize;
59    size_t infoSize;
60    size_t totalMemory;
61    size_t backtraceSize;
62    get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize);
63
64    size_t count;
65    if (info == nullptr || overallSize == 0 || infoSize == 0
66            || (count = overallSize / infoSize) == 0) {
67        ALOGD("no malloc info, libc.debug.malloc.program property should be set");
68        return std::string();
69    }
70
71    std::ostringstream oss;
72    oss << totalMemory << " bytes in " << count << " allocations\n";
73    oss << "  ABI: '" ABI_STRING "'" << "\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)
89                << " bytes ( " << e->size << " bytes * " << e->allocations << " 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 android
98