James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 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 | |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 17 | |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MemoryLeackTrackUtil" |
| 20 | #include <utils/Log.h> |
| 21 | |
Andy Hung | e9eb03e | 2020-04-04 14:08:23 -0700 | [diff] [blame] | 22 | #include <mediautils/MemoryLeakTrackUtil.h> |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 23 | #include <sstream> |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 24 | |
Christopher Ferris | 7a3180d | 2019-09-11 19:08:13 -0700 | [diff] [blame] | 25 | #include <bionic/malloc.h> |
Christopher Ferris | bd0195e | 2019-04-17 13:26:15 -0700 | [diff] [blame] | 26 | |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 27 | /* |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 28 | * The code here originally resided in MediaPlayerService.cpp |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 29 | */ |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 30 | |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 31 | // Figure out the abi based on defined macros. |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 32 | #if defined(__arm__) |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 33 | #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 | |
| 48 | extern std::string backtrace_string(const uintptr_t* frames, size_t frame_count); |
| 49 | |
| 50 | namespace android { |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 51 | |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 52 | std::string dumpMemoryAddresses(size_t limit) |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 53 | { |
Christopher Ferris | bd0195e | 2019-04-17 13:26:15 -0700 | [diff] [blame] | 54 | 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 Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 58 | |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 59 | size_t count; |
Christopher Ferris | bd0195e | 2019-04-17 13:26:15 -0700 | [diff] [blame] | 60 | 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 Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 62 | ALOGD("no malloc info, libc.debug.malloc.program property should be set"); |
Christopher Ferris | bd0195e | 2019-04-17 13:26:15 -0700 | [diff] [blame] | 63 | return ""; |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 64 | } |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 65 | |
| 66 | std::ostringstream oss; |
Christopher Ferris | bd0195e | 2019-04-17 13:26:15 -0700 | [diff] [blame] | 67 | oss << leak_info.total_memory << " bytes in " << count << " allocations\n"; |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 68 | 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 Ferris | bd0195e | 2019-04-17 13:26:15 -0700 | [diff] [blame] | 81 | const AllocEntry * const e = (AllocEntry *)(leak_info.buffer + i * leak_info.info_size); |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 82 | |
| 83 | oss << (e->size * e->allocations) |
| 84 | << " bytes ( " << e->size << " bytes * " << e->allocations << " allocations )\n"; |
Christopher Ferris | bd0195e | 2019-04-17 13:26:15 -0700 | [diff] [blame] | 85 | oss << backtrace_string(e->backtrace, leak_info.backtrace_size) << "\n"; |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 86 | } |
| 87 | oss << "\n"; |
Christopher Ferris | bd0195e | 2019-04-17 13:26:15 -0700 | [diff] [blame] | 88 | android_mallopt(M_FREE_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info)); |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 89 | return oss.str(); |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 90 | } |
| 91 | |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 92 | } // namespace android |