blob: 991eb4faa1b25f66c8a08bf6a854e0e993987b2e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
Andy McFadden06a6b552010-07-13 16:28:09 -070017#define LOG_TAG "android.os.Debug"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018#include "JNIHelp.h"
19#include "jni.h"
Dianne Hackbornf72467a2012-06-08 17:23:59 -070020#include <utils/String8.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include "utils/misc.h"
Dianne Hackbornf72467a2012-06-08 17:23:59 -070022#include "cutils/debugger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
Dianne Hackbornf72467a2012-06-08 17:23:59 -070024#include <fcntl.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29#include <time.h>
30#include <sys/time.h>
Andy McFadden06a6b552010-07-13 16:28:09 -070031#include <errno.h>
32#include <assert.h>
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070033#include <ctype.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35#ifdef HAVE_MALLOC_H
36#include <malloc.h>
37#endif
38
39namespace android
40{
41
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070042enum {
43 HEAP_UNKNOWN,
44 HEAP_DALVIK,
45 HEAP_NATIVE,
Ian Rogers7c9f30b2013-02-27 10:57:13 -080046 HEAP_STACK,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070047 HEAP_CURSOR,
48 HEAP_ASHMEM,
49 HEAP_UNKNOWN_DEV,
50 HEAP_SO,
51 HEAP_JAR,
52 HEAP_APK,
53 HEAP_TTF,
54 HEAP_DEX,
Anwar Ghuloum8884ef42013-03-15 12:56:59 -070055 HEAP_OAT,
Anwar Ghuloum88887d02013-03-19 15:30:12 -070056 HEAP_ART,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070057 HEAP_UNKNOWN_MAP,
58
59 _NUM_HEAP,
60 _NUM_CORE_HEAP = HEAP_NATIVE+1
61};
62
63struct stat_fields {
64 jfieldID pss_field;
65 jfieldID privateDirty_field;
66 jfieldID sharedDirty_field;
67};
68
69struct stat_field_names {
70 const char* pss_name;
71 const char* privateDirty_name;
72 const char* sharedDirty_name;
73};
74
75static stat_fields stat_fields[_NUM_CORE_HEAP];
76
77static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
78 { "otherPss", "otherPrivateDirty", "otherSharedDirty" },
79 { "dalvikPss", "dalvikPrivateDirty", "dalvikSharedDirty" },
80 { "nativePss", "nativePrivateDirty", "nativeSharedDirty" }
81};
82
83jfieldID otherStats_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070086 int pss;
87 int privateDirty;
88 int sharedDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089};
90
91#define BINDER_STATS "/proc/binder/stats"
92
93static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
94{
95#ifdef HAVE_MALLOC_H
96 struct mallinfo info = mallinfo();
97 return (jlong) info.usmblks;
98#else
99 return -1;
100#endif
101}
102
103static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
104{
105#ifdef HAVE_MALLOC_H
106 struct mallinfo info = mallinfo();
107 return (jlong) info.uordblks;
108#else
109 return -1;
110#endif
111}
112
113static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
114{
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800115#ifdef HAVE_MALLOC_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 struct mallinfo info = mallinfo();
117 return (jlong) info.fordblks;
118#else
119 return -1;
120#endif
121}
122
123static void read_mapinfo(FILE *fp, stats_t* stats)
124{
125 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700126 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 bool skip, done = false;
128
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700129 unsigned size = 0, resident = 0, pss = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 unsigned shared_clean = 0, shared_dirty = 0;
131 unsigned private_clean = 0, private_dirty = 0;
132 unsigned referenced = 0;
133 unsigned temp;
134
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700135 unsigned long int start;
136 unsigned long int end = 0;
137 unsigned long int prevEnd = 0;
138 char* name;
139 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700141 int whichHeap = HEAP_UNKNOWN;
142 int prevHeap = HEAP_UNKNOWN;
143
144 if(fgets(line, sizeof(line), fp) == 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
146 while (!done) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700147 prevHeap = whichHeap;
148 prevEnd = end;
149 whichHeap = HEAP_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 skip = false;
151
152 len = strlen(line);
153 if (len < 1) return;
154 line[--len] = 0;
155
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700156 if (sscanf(line, "%lx-%lx %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
157 skip = true;
158 } else {
159 while (isspace(line[name_pos])) {
160 name_pos += 1;
161 }
162 name = line + name_pos;
163 nameLen = strlen(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Ian Rogersafc10e12013-02-22 09:40:58 -0800165 if ((strstr(name, "[heap]") == name) ||
166 (strstr(name, "/dev/ashmem/libc malloc") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700167 whichHeap = HEAP_NATIVE;
168 } else if (strstr(name, "/dev/ashmem/dalvik-") == name) {
169 whichHeap = HEAP_DALVIK;
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800170 } else if (strstr(name, "[stack") == name) {
171 whichHeap = HEAP_STACK;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700172 } else if (strstr(name, "/dev/ashmem/CursorWindow") == name) {
173 whichHeap = HEAP_CURSOR;
174 } else if (strstr(name, "/dev/ashmem/") == name) {
175 whichHeap = HEAP_ASHMEM;
176 } else if (strstr(name, "/dev/") == name) {
177 whichHeap = HEAP_UNKNOWN_DEV;
178 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
179 whichHeap = HEAP_SO;
180 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) {
181 whichHeap = HEAP_JAR;
182 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) {
183 whichHeap = HEAP_APK;
184 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) {
185 whichHeap = HEAP_TTF;
Ian Rogers9f8589c2013-02-22 20:35:06 -0800186 } else if ((nameLen > 4 && strcmp(name+nameLen-4, ".dex") == 0) ||
187 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700188 whichHeap = HEAP_DEX;
Anwar Ghuloum8884ef42013-03-15 12:56:59 -0700189 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) {
190 whichHeap = HEAP_OAT;
Anwar Ghuloum88887d02013-03-19 15:30:12 -0700191 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) {
192 whichHeap = HEAP_ART;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700193 } else if (nameLen > 0) {
194 whichHeap = HEAP_UNKNOWN_MAP;
195 } else if (start == prevEnd && prevHeap == HEAP_SO) {
196 // bss section of a shared library.
197 whichHeap = HEAP_SO;
198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
200
Steve Block6215d3f2012-01-04 20:05:49 +0000201 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 // isSqliteHeap, line);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 while (true) {
205 if (fgets(line, 1024, fp) == 0) {
206 done = true;
207 break;
208 }
209
210 if (sscanf(line, "Size: %d kB", &temp) == 1) {
211 size = temp;
212 } else if (sscanf(line, "Rss: %d kB", &temp) == 1) {
213 resident = temp;
214 } else if (sscanf(line, "Pss: %d kB", &temp) == 1) {
215 pss = temp;
216 } else if (sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
217 shared_clean = temp;
218 } else if (sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
219 shared_dirty = temp;
220 } else if (sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
221 private_clean = temp;
222 } else if (sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
223 private_dirty = temp;
224 } else if (sscanf(line, "Referenced: %d kB", &temp) == 1) {
225 referenced = temp;
Grace Klobabd511162009-07-08 23:32:25 -0700226 } else if (strlen(line) > 30 && line[8] == '-' && line[17] == ' ') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700228 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 break;
230 }
231 }
232
233 if (!skip) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700234 stats[whichHeap].pss += pss;
235 stats[whichHeap].privateDirty += private_dirty;
236 stats[whichHeap].sharedDirty += shared_dirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 }
238 }
239}
240
241static void load_maps(int pid, stats_t* stats)
242{
243 char tmp[128];
244 FILE *fp;
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 sprintf(tmp, "/proc/%d/smaps", pid);
247 fp = fopen(tmp, "r");
248 if (fp == 0) return;
249
250 read_mapinfo(fp, stats);
251 fclose(fp);
252}
253
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700254static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
255 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256{
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700257 stats_t stats[_NUM_HEAP];
258 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800259
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700260 load_maps(pid, stats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700262 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
263 stats[HEAP_UNKNOWN].pss += stats[i].pss;
264 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
265 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
266 }
267
268 for (int i=0; i<_NUM_CORE_HEAP; i++) {
269 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
270 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
271 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
272 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800273
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700274 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800275
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700276 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
277 if (otherArray == NULL) {
278 return;
279 }
280
281 int j=0;
282 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
283 otherArray[j++] = stats[i].pss;
284 otherArray[j++] = stats[i].privateDirty;
285 otherArray[j++] = stats[i].sharedDirty;
286 }
287
288 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289}
290
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700291static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
292{
293 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
294}
295
Dianne Hackbornb437e092011-08-05 17:50:29 -0700296static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid)
297{
298 char line[1024];
299 jlong pss = 0;
300 unsigned temp;
301
302 char tmp[128];
303 FILE *fp;
304
305 sprintf(tmp, "/proc/%d/smaps", pid);
306 fp = fopen(tmp, "r");
307 if (fp == 0) return 0;
308
309 while (true) {
310 if (fgets(line, 1024, fp) == 0) {
311 break;
312 }
313
314 if (sscanf(line, "Pss: %d kB", &temp) == 1) {
315 pss += temp;
316 }
317 }
318
319 fclose(fp);
320
321 return pss;
322}
323
324static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
325{
326 return android_os_Debug_getPssPid(env, clazz, getpid());
327}
328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329static jint read_binder_stat(const char* stat)
330{
331 FILE* fp = fopen(BINDER_STATS, "r");
332 if (fp == NULL) {
333 return -1;
334 }
335
336 char line[1024];
337
338 char compare[128];
339 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 // loop until we have the block that represents this process
342 do {
343 if (fgets(line, 1024, fp) == 0) {
344 return -1;
345 }
346 } while (strncmp(compare, line, len));
347
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800348 // now that we have this process, read until we find the stat that we are looking for
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 do {
352 if (fgets(line, 1024, fp) == 0) {
353 return -1;
354 }
355 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 // we have the line, now increment the line ptr to the value
358 char* ptr = line + len;
359 return atoi(ptr);
360}
361
362static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
363{
364 return read_binder_stat("bcTRANSACTION");
365}
366
367static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
368{
369 return read_binder_stat("brTRANSACTION");
370}
371
372// these are implemented in android_util_Binder.cpp
373jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
374jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
375jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
376
Andy McFadden06a6b552010-07-13 16:28:09 -0700377
Andy McFadden06a6b552010-07-13 16:28:09 -0700378/* pulled out of bionic */
379extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
380 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
381extern "C" void free_malloc_leak_info(uint8_t* info);
382#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
383#define BACKTRACE_SIZE 32
384
385/*
386 * This is a qsort() callback.
387 *
388 * See dumpNativeHeap() for comments about the data format and sort order.
389 */
390static int compareHeapRecords(const void* vrec1, const void* vrec2)
391{
392 const size_t* rec1 = (const size_t*) vrec1;
393 const size_t* rec2 = (const size_t*) vrec2;
394 size_t size1 = *rec1;
395 size_t size2 = *rec2;
396
397 if (size1 < size2) {
398 return 1;
399 } else if (size1 > size2) {
400 return -1;
401 }
402
403 intptr_t* bt1 = (intptr_t*)(rec1 + 2);
404 intptr_t* bt2 = (intptr_t*)(rec2 + 2);
405 for (size_t idx = 0; idx < BACKTRACE_SIZE; idx++) {
406 intptr_t addr1 = bt1[idx];
407 intptr_t addr2 = bt2[idx];
408 if (addr1 == addr2) {
409 if (addr1 == 0)
410 break;
411 continue;
412 }
413 if (addr1 < addr2) {
414 return -1;
415 } else if (addr1 > addr2) {
416 return 1;
417 }
418 }
419
420 return 0;
421}
422
423/*
424 * The get_malloc_leak_info() call returns an array of structs that
425 * look like this:
426 *
427 * size_t size
428 * size_t allocations
429 * intptr_t backtrace[32]
430 *
431 * "size" is the size of the allocation, "backtrace" is a fixed-size
432 * array of function pointers, and "allocations" is the number of
433 * allocations with the exact same size and backtrace.
434 *
435 * The entries are sorted by descending total size (i.e. size*allocations)
436 * then allocation count. For best results with "diff" we'd like to sort
437 * primarily by individual size then stack trace. Since the entries are
438 * fixed-size, and we're allowed (by the current implementation) to mangle
439 * them, we can do this in place.
440 */
441static void dumpNativeHeap(FILE* fp)
442{
443 uint8_t* info = NULL;
444 size_t overallSize, infoSize, totalMemory, backtraceSize;
445
446 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory,
447 &backtraceSize);
448 if (info == NULL) {
449 fprintf(fp, "Native heap dump not available. To enable, run these"
450 " commands (requires root):\n");
451 fprintf(fp, "$ adb shell setprop libc.debug.malloc 1\n");
452 fprintf(fp, "$ adb shell stop\n");
453 fprintf(fp, "$ adb shell start\n");
454 return;
455 }
456 assert(infoSize != 0);
457 assert(overallSize % infoSize == 0);
458
459 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
460
461 size_t recordCount = overallSize / infoSize;
462 fprintf(fp, "Total memory: %zu\n", totalMemory);
463 fprintf(fp, "Allocation records: %zd\n", recordCount);
464 if (backtraceSize != BACKTRACE_SIZE) {
465 fprintf(fp, "WARNING: mismatched backtrace sizes (%d vs. %d)\n",
466 backtraceSize, BACKTRACE_SIZE);
467 }
468 fprintf(fp, "\n");
469
470 /* re-sort the entries */
471 qsort(info, recordCount, infoSize, compareHeapRecords);
472
473 /* dump the entries to the file */
474 const uint8_t* ptr = info;
475 for (size_t idx = 0; idx < recordCount; idx++) {
476 size_t size = *(size_t*) ptr;
477 size_t allocations = *(size_t*) (ptr + sizeof(size_t));
478 intptr_t* backtrace = (intptr_t*) (ptr + sizeof(size_t) * 2);
479
480 fprintf(fp, "z %d sz %8zu num %4zu bt",
481 (size & SIZE_FLAG_ZYGOTE_CHILD) != 0,
482 size & ~SIZE_FLAG_ZYGOTE_CHILD,
483 allocations);
484 for (size_t bt = 0; bt < backtraceSize; bt++) {
485 if (backtrace[bt] == 0) {
486 break;
487 } else {
488 fprintf(fp, " %08x", backtrace[bt]);
489 }
490 }
491 fprintf(fp, "\n");
492
493 ptr += infoSize;
494 }
495
Andy McFadden06a6b552010-07-13 16:28:09 -0700496 free_malloc_leak_info(info);
Brian Carlstrom393b84c12010-10-17 23:40:43 -0700497
498 fprintf(fp, "MAPS\n");
499 const char* maps = "/proc/self/maps";
500 FILE* in = fopen(maps, "r");
501 if (in == NULL) {
502 fprintf(fp, "Could not open %s\n", maps);
503 return;
504 }
505 char buf[BUFSIZ];
506 while (size_t n = fread(buf, sizeof(char), BUFSIZ, in)) {
507 fwrite(buf, sizeof(char), n, fp);
508 }
509 fclose(in);
510
511 fprintf(fp, "END\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700512}
Andy McFadden06a6b552010-07-13 16:28:09 -0700513
514/*
515 * Dump the native heap, writing human-readable output to the specified
516 * file descriptor.
517 */
518static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz,
519 jobject fileDescriptor)
520{
521 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800522 jniThrowNullPointerException(env, "fd == null");
Andy McFadden06a6b552010-07-13 16:28:09 -0700523 return;
524 }
525 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
526 if (origFd < 0) {
527 jniThrowRuntimeException(env, "Invalid file descriptor");
528 return;
529 }
530
531 /* dup() the descriptor so we don't close the original with fclose() */
532 int fd = dup(origFd);
533 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000534 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700535 jniThrowRuntimeException(env, "dup() failed");
536 return;
537 }
538
539 FILE* fp = fdopen(fd, "w");
540 if (fp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000541 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700542 close(fd);
543 jniThrowRuntimeException(env, "fdopen() failed");
544 return;
545 }
546
Steve Block5baa3a62011-12-20 16:23:08 +0000547 ALOGD("Native heap dump starting...\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700548 dumpNativeHeap(fp);
Steve Block5baa3a62011-12-20 16:23:08 +0000549 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700550
551 fclose(fp);
552}
553
554
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700555static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz,
556 jint pid, jstring fileName)
557{
558 if (fileName == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800559 jniThrowNullPointerException(env, "file == null");
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700560 return;
561 }
562 const jchar* str = env->GetStringCritical(fileName, 0);
563 String8 fileName8;
564 if (str) {
565 fileName8 = String8(str, env->GetStringLength(fileName));
566 env->ReleaseStringCritical(fileName, str);
567 }
568
569 int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
570 if (fd < 0) {
571 fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno));
572 return;
573 }
574
575 if (lseek(fd, 0, SEEK_END) < 0) {
576 fprintf(stderr, "lseek: %s\n", strerror(errno));
577 } else {
578 dump_backtrace_to_file(pid, fd);
579 }
580
581 close(fd);
582}
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584/*
585 * JNI registration.
586 */
587
588static JNINativeMethod gMethods[] = {
589 { "getNativeHeapSize", "()J",
590 (void*) android_os_Debug_getNativeHeapSize },
591 { "getNativeHeapAllocatedSize", "()J",
592 (void*) android_os_Debug_getNativeHeapAllocatedSize },
593 { "getNativeHeapFreeSize", "()J",
594 (void*) android_os_Debug_getNativeHeapFreeSize },
595 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
596 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700597 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
598 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -0700599 { "getPss", "()J",
600 (void*) android_os_Debug_getPss },
601 { "getPss", "(I)J",
602 (void*) android_os_Debug_getPssPid },
Andy McFadden06a6b552010-07-13 16:28:09 -0700603 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
604 (void*) android_os_Debug_dumpNativeHeap },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 { "getBinderSentTransactions", "()I",
606 (void*) android_os_Debug_getBinderSentTransactions },
607 { "getBinderReceivedTransactions", "()I",
608 (void*) android_os_getBinderReceivedTransactions },
609 { "getBinderLocalObjectCount", "()I",
610 (void*)android_os_Debug_getLocalObjectCount },
611 { "getBinderProxyObjectCount", "()I",
612 (void*)android_os_Debug_getProxyObjectCount },
613 { "getBinderDeathObjectCount", "()I",
614 (void*)android_os_Debug_getDeathObjectCount },
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700615 { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V",
616 (void*)android_os_Debug_dumpNativeBacktraceToFile },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617};
618
619int register_android_os_Debug(JNIEnv *env)
620{
621 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800623 // Sanity check the number of other statistics expected in Java matches here.
624 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
625 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
626 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
627 if (numOtherStats != expectedNumOtherStats) {
628 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
629 "android.os.Debug.Meminfo.NUM_OTHER_STATS=%d expected %d",
630 numOtherStats, expectedNumOtherStats);
631 return JNI_ERR;
632 }
633
634 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
635
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700636 for (int i=0; i<_NUM_CORE_HEAP; i++) {
637 stat_fields[i].pss_field =
638 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
639 stat_fields[i].privateDirty_field =
640 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
641 stat_fields[i].sharedDirty_field =
642 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
643 }
644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
646}
647
Andy McFadden06a6b552010-07-13 16:28:09 -0700648}; // namespace android