Home | History | Annotate | Download | only in utils
      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 #include <bionic_malloc.h>
     26 
     27 /*
     28  * The code here originally resided in MediaPlayerService.cpp
     29  */
     30 
     31 // Figure out the abi based on defined macros.
     32 #if defined(__arm__)
     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 {
     51 
     52 std::string dumpMemoryAddresses(size_t limit)
     53 {
     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     }
     58 
     59     size_t count;
     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) {
     62         ALOGD("no malloc info, libc.debug.malloc.program property should be set");
     63         return "";
     64     }
     65 
     66     std::ostringstream oss;
     67     oss << leak_info.total_memory << " bytes in " << count << " allocations\n";
     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 
     81         const AllocEntry * const e = (AllocEntry *)(leak_info.buffer + i * leak_info.info_size);
     82 
     83         oss << (e->size * e->allocations)
     84                 << " bytes ( " << e->size << " bytes * " << e->allocations << " allocations )\n";
     85         oss << backtrace_string(e->backtrace, leak_info.backtrace_size) << "\n";
     86     }
     87     oss << "\n";
     88     android_mallopt(M_FREE_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info));
     89     return oss.str();
     90 }
     91 
     92 }  // namespace android
     93