blob: 18f5f2502c2c75c2892081afd725e9eb8289a480 [file] [log] [blame]
James Dong8635b7b2011-03-14 17:01:38 -07001/*
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
James Dong8635b7b2011-03-14 17:01:38 -070017
Andy Hung07b745e2016-05-23 16:21:07 -070018//#define LOG_NDEBUG 0
19#define LOG_TAG "MemoryLeackTrackUtil"
20#include <utils/Log.h>
21
22#include "media/MemoryLeakTrackUtil.h"
23#include <sstream>
James Dong8635b7b2011-03-14 17:01:38 -070024
25/*
Andy Hung07b745e2016-05-23 16:21:07 -070026 * The code here originally resided in MediaPlayerService.cpp
James Dong8635b7b2011-03-14 17:01:38 -070027 */
James Dong8635b7b2011-03-14 17:01:38 -070028
Andy Hung07b745e2016-05-23 16:21:07 -070029// Figure out the abi based on defined macros.
James Dong8635b7b2011-03-14 17:01:38 -070030#if defined(__arm__)
Andy Hung07b745e2016-05-23 16:21:07 -070031#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 {
James Dong8635b7b2011-03-14 17:01:38 -070049
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
Andy Hung07b745e2016-05-23 16:21:07 -070055std::string dumpMemoryAddresses(size_t limit)
James Dong8635b7b2011-03-14 17:01:38 -070056{
Andy Hung07b745e2016-05-23 16:21:07 -070057 uint8_t *info;
58 size_t overallSize;
59 size_t infoSize;
60 size_t totalMemory;
61 size_t backtraceSize;
James Dong8635b7b2011-03-14 17:01:38 -070062 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize);
James Dong8635b7b2011-03-14 17:01:38 -070063
Andy Hung07b745e2016-05-23 16:21:07 -070064 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();
James Dong8635b7b2011-03-14 17:01:38 -070069 }
Andy Hung07b745e2016-05-23 16:21:07 -070070
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();
James Dong8635b7b2011-03-14 17:01:38 -070095}
96
James Dong8635b7b2011-03-14 17:01:38 -070097} // namespace android