blob: 74510332df51d97fbd47c4b1e14719481edb1a6f [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"
Chen Guoyincab8e532022-10-12 23:10:26 +080036#elif defined(__riscv)
37#define ABI_STRING "riscv64"
Andy Hung07b745e2016-05-23 16:21:07 -070038#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
Andy Hung07b745e2016-05-23 16:21:07 -070050std::string dumpMemoryAddresses(size_t limit)
James Dong8635b7b2011-03-14 17:01:38 -070051{
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070052 android_mallopt_leak_info_t leak_info;
53 if (!android_mallopt(M_GET_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info))) {
54 return "";
55 }
James Dong8635b7b2011-03-14 17:01:38 -070056
Andy Hung07b745e2016-05-23 16:21:07 -070057 size_t count;
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070058 if (leak_info.buffer == nullptr || leak_info.overall_size == 0 || leak_info.info_size == 0
59 || (count = leak_info.overall_size / leak_info.info_size) == 0) {
Andy Hung07b745e2016-05-23 16:21:07 -070060 ALOGD("no malloc info, libc.debug.malloc.program property should be set");
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070061 return "";
James Dong8635b7b2011-03-14 17:01:38 -070062 }
Andy Hung07b745e2016-05-23 16:21:07 -070063
64 std::ostringstream oss;
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070065 oss << leak_info.total_memory << " bytes in " << count << " allocations\n";
Andy Hung07b745e2016-05-23 16:21:07 -070066 oss << " ABI: '" ABI_STRING "'" << "\n\n";
67 if (count > limit) count = limit;
68
69 // The memory is sorted based on total size which is useful for finding
70 // worst memory offenders. For diffs, sometimes it is preferable to sort
71 // based on the backtrace.
72 for (size_t i = 0; i < count; i++) {
73 struct AllocEntry {
74 size_t size; // bit 31 is set if this is zygote allocated memory
75 size_t allocations;
76 uintptr_t backtrace[];
77 };
78
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070079 const AllocEntry * const e = (AllocEntry *)(leak_info.buffer + i * leak_info.info_size);
Andy Hung07b745e2016-05-23 16:21:07 -070080
81 oss << (e->size * e->allocations)
82 << " bytes ( " << e->size << " bytes * " << e->allocations << " allocations )\n";
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070083 oss << backtrace_string(e->backtrace, leak_info.backtrace_size) << "\n";
Andy Hung07b745e2016-05-23 16:21:07 -070084 }
85 oss << "\n";
Christopher Ferrisbd0195e2019-04-17 13:26:15 -070086 android_mallopt(M_FREE_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info));
Andy Hung07b745e2016-05-23 16:21:07 -070087 return oss.str();
James Dong8635b7b2011-03-14 17:01:38 -070088}
89
James Dong8635b7b2011-03-14 17:01:38 -070090} // namespace android