blob: fdb8c4fb922e8bf4f4ec028481fd2445610720c9 [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
Andy Hunge9eb03e2020-04-04 14:08:23 -070022#include <mediautils/MemoryLeakTrackUtil.h>
Andy Hung07b745e2016-05-23 16:21:07 -070023#include <sstream>
James Dong8635b7b2011-03-14 17:01:38 -070024
Christopher Ferris7a3180d2019-09-11 19:08:13 -070025#include <bionic/malloc.h>
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070026
James Dong8635b7b2011-03-14 17:01:38 -070027/*
Andy Hung07b745e2016-05-23 16:21:07 -070028 * The code here originally resided in MediaPlayerService.cpp
James Dong8635b7b2011-03-14 17:01:38 -070029 */
James Dong8635b7b2011-03-14 17:01:38 -070030
Andy Hung07b745e2016-05-23 16:21:07 -070031// Figure out the abi based on defined macros.
James Dong8635b7b2011-03-14 17:01:38 -070032#if defined(__arm__)
Andy Hung07b745e2016-05-23 16:21:07 -070033#define ABI_STRING "arm"
34#elif defined(__aarch64__)
35#define ABI_STRING "arm64"
36#elif defined(__mips__) && !defined(__LP64__)
37#define ABI_STRING "mips"
38#elif defined(__mips__) && defined(__LP64__)
39#define ABI_STRING "mips64"
40#elif defined(__i386__)
41#define ABI_STRING "x86"
42#elif defined(__x86_64__)
43#define ABI_STRING "x86_64"
44#else
45#error "Unsupported ABI"
46#endif
47
48extern std::string backtrace_string(const uintptr_t* frames, size_t frame_count);
49
50namespace android {
James Dong8635b7b2011-03-14 17:01:38 -070051
Andy Hung07b745e2016-05-23 16:21:07 -070052std::string dumpMemoryAddresses(size_t limit)
James Dong8635b7b2011-03-14 17:01:38 -070053{
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070054 android_mallopt_leak_info_t leak_info;
55 if (!android_mallopt(M_GET_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info))) {
56 return "";
57 }
James Dong8635b7b2011-03-14 17:01:38 -070058
Andy Hung07b745e2016-05-23 16:21:07 -070059 size_t count;
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070060 if (leak_info.buffer == nullptr || leak_info.overall_size == 0 || leak_info.info_size == 0
61 || (count = leak_info.overall_size / leak_info.info_size) == 0) {
Andy Hung07b745e2016-05-23 16:21:07 -070062 ALOGD("no malloc info, libc.debug.malloc.program property should be set");
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070063 return "";
James Dong8635b7b2011-03-14 17:01:38 -070064 }
Andy Hung07b745e2016-05-23 16:21:07 -070065
66 std::ostringstream oss;
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070067 oss << leak_info.total_memory << " bytes in " << count << " allocations\n";
Andy Hung07b745e2016-05-23 16:21:07 -070068 oss << " ABI: '" ABI_STRING "'" << "\n\n";
69 if (count > limit) count = limit;
70
71 // The memory is sorted based on total size which is useful for finding
72 // worst memory offenders. For diffs, sometimes it is preferable to sort
73 // based on the backtrace.
74 for (size_t i = 0; i < count; i++) {
75 struct AllocEntry {
76 size_t size; // bit 31 is set if this is zygote allocated memory
77 size_t allocations;
78 uintptr_t backtrace[];
79 };
80
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070081 const AllocEntry * const e = (AllocEntry *)(leak_info.buffer + i * leak_info.info_size);
Andy Hung07b745e2016-05-23 16:21:07 -070082
83 oss << (e->size * e->allocations)
84 << " bytes ( " << e->size << " bytes * " << e->allocations << " allocations )\n";
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070085 oss << backtrace_string(e->backtrace, leak_info.backtrace_size) << "\n";
Andy Hung07b745e2016-05-23 16:21:07 -070086 }
87 oss << "\n";
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070088 android_mallopt(M_FREE_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info));
Andy Hung07b745e2016-05-23 16:21:07 -070089 return oss.str();
James Dong8635b7b2011-03-14 17:01:38 -070090}
91
James Dong8635b7b2011-03-14 17:01:38 -070092} // namespace android