1 /* //device/libs/android_runtime/android_ddm_DdmHandleNativeHeap.cpp
3 ** Copyright 2006, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
19 #define LOG_TAG "DdmHandleNativeHeap"
23 #include "core_jni_helpers.h"
25 #include <utils/Log.h>
26 #include <utils/String8.h>
30 #include <sys/types.h>
33 extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
34 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
36 extern "C" void free_malloc_leak_info(uint8_t* info);
38 #define DDMS_HEADER_SIGNATURE 0x812345dd
39 #define DDMS_VERSION 2
56 static void ReadFile(const char* path, String8& s) {
57 int fd = open(path, O_RDONLY);
61 while ((byteCount = TEMP_FAILURE_RETRY(read(fd, bytes, sizeof(bytes)))) > 0) {
62 s.append(bytes, byteCount);
69 * Retrieve the native heap information and the info from /proc/self/maps,
70 * copy them into a byte[] with a "struct Header" that holds data offsets,
71 * and return the array.
73 static jbyteArray DdmHandleNativeHeap_getLeakInfo(JNIEnv* env, jobject) {
75 memset(&header, 0, sizeof(header));
78 ReadFile("/proc/self/maps", maps);
79 header.mapSize = maps.size();
82 get_malloc_leak_info(&allocBytes, &header.allocSize, &header.allocInfoSize,
83 &header.totalMemory, &header.backtraceSize);
85 ALOGD("*** mapSize: %zu allocSize: %zu allocInfoSize: %zu totalMemory: %zu",
86 header.mapSize, header.allocSize, header.allocInfoSize, header.totalMemory);
89 header.signature = DDMS_HEADER_SIGNATURE;
90 header.version = DDMS_VERSION;
91 header.pointerSize = sizeof(void*);
94 jbyteArray array = env->NewByteArray(sizeof(Header) + header.mapSize + header.allocSize);
96 env->SetByteArrayRegion(array, 0,
97 sizeof(header), reinterpret_cast<jbyte*>(&header));
98 env->SetByteArrayRegion(array, sizeof(header),
99 maps.size(), reinterpret_cast<const jbyte*>(maps.string()));
100 env->SetByteArrayRegion(array, sizeof(header) + maps.size(),
101 header.allocSize, reinterpret_cast<jbyte*>(allocBytes));
104 free_malloc_leak_info(allocBytes);
108 static JNINativeMethod method_table[] = {
109 { "getLeakInfo", "()[B", (void*) DdmHandleNativeHeap_getLeakInfo },
112 int register_android_ddm_DdmHandleNativeHeap(JNIEnv* env) {
113 return RegisterMethodsOrDie(env, "android/ddm/DdmHandleNativeHeap", method_table,
114 NELEM(method_table));