blob: f5eaf94b15d289bbcaecb312256b646af5a4a42a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/libs/android_runtime/android_ddm_DdmHandleNativeHeap.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
Elliott Hughesa480daf2013-03-26 13:18:52 -07005** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008**
Elliott Hughesa480daf2013-03-26 13:18:52 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Elliott Hughesa480daf2013-03-26 13:18:52 -070011** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
18#undef LOG_TAG
19#define LOG_TAG "DdmHandleNativeHeap"
20
21#include <JNIHelp.h>
22#include <jni.h>
23#include <android_runtime/AndroidRuntime.h>
24
25#include <utils/Log.h>
Elliott Hughesa480daf2013-03-26 13:18:52 -070026#include <utils/String8.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
28#include <fcntl.h>
29#include <errno.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32
Elliott Hughesa480daf2013-03-26 13:18:52 -070033extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
34 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
Elliott Hughesa480daf2013-03-26 13:18:52 -070036extern "C" void free_malloc_leak_info(uint8_t* info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38struct Header {
39 size_t mapSize;
40 size_t allocSize;
41 size_t allocInfoSize;
42 size_t totalMemory;
43 size_t backtraceSize;
44};
45
46namespace android {
47
Elliott Hughesa480daf2013-03-26 13:18:52 -070048static void ReadFile(const char* path, String8& s) {
49 int fd = open(path, O_RDONLY);
50 if (fd != -1) {
51 char bytes[1024];
52 ssize_t byteCount;
53 while ((byteCount = TEMP_FAILURE_RETRY(read(fd, bytes, sizeof(bytes)))) > 0) {
54 s.append(bytes, byteCount);
55 }
56 close(fd);
57 }
58}
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060/*
Elliott Hughesa480daf2013-03-26 13:18:52 -070061 * Retrieve the native heap information and the info from /proc/self/maps,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 * copy them into a byte[] with a "struct Header" that holds data offsets,
63 * and return the array.
64 */
Elliott Hughesa480daf2013-03-26 13:18:52 -070065static jbyteArray DdmHandleNativeHeap_getLeakInfo(JNIEnv* env, jobject) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 Header header;
67 memset(&header, 0, sizeof(header));
68
Elliott Hughesa480daf2013-03-26 13:18:52 -070069 String8 maps;
70 ReadFile("/proc/self/maps", maps);
71 header.mapSize = maps.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73 uint8_t* allocBytes;
Elliott Hughesa480daf2013-03-26 13:18:52 -070074 get_malloc_leak_info(&allocBytes, &header.allocSize, &header.allocInfoSize,
75 &header.totalMemory, &header.backtraceSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
Elliott Hughesa480daf2013-03-26 13:18:52 -070077 ALOGD("*** mapSize: %d allocSize: %d allocInfoSize: %d totalMemory: %d",
78 header.mapSize, header.allocSize, header.allocInfoSize, header.totalMemory);
79
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 jbyteArray array = env->NewByteArray(sizeof(Header) + header.mapSize + header.allocSize);
Elliott Hughesa480daf2013-03-26 13:18:52 -070081 if (array != NULL) {
82 env->SetByteArrayRegion(array, 0,
83 sizeof(header), reinterpret_cast<jbyte*>(&header));
84 env->SetByteArrayRegion(array, sizeof(header),
85 maps.size(), reinterpret_cast<const jbyte*>(maps.string()));
86 env->SetByteArrayRegion(array, sizeof(header) + maps.size(),
87 header.allocSize, reinterpret_cast<jbyte*>(allocBytes));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 }
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 free_malloc_leak_info(allocBytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 return array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092}
93
94static JNINativeMethod method_table[] = {
Elliott Hughesa480daf2013-03-26 13:18:52 -070095 { "getLeakInfo", "()[B", (void*) DdmHandleNativeHeap_getLeakInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096};
97
Elliott Hughesa480daf2013-03-26 13:18:52 -070098int register_android_ddm_DdmHandleNativeHeap(JNIEnv* env) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 return AndroidRuntime::registerNativeMethods(env, "android/ddm/DdmHandleNativeHeap", method_table, NELEM(method_table));
100}
101
102};