blob: 6e21a1123e7bbd9bbb5474ff7824e2fb978b9eeb [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,
46 HEAP_CURSOR,
47 HEAP_ASHMEM,
48 HEAP_UNKNOWN_DEV,
49 HEAP_SO,
50 HEAP_JAR,
51 HEAP_APK,
52 HEAP_TTF,
53 HEAP_DEX,
54 HEAP_UNKNOWN_MAP,
55
56 _NUM_HEAP,
57 _NUM_CORE_HEAP = HEAP_NATIVE+1
58};
59
60struct stat_fields {
61 jfieldID pss_field;
62 jfieldID privateDirty_field;
63 jfieldID sharedDirty_field;
64};
65
66struct stat_field_names {
67 const char* pss_name;
68 const char* privateDirty_name;
69 const char* sharedDirty_name;
70};
71
72static stat_fields stat_fields[_NUM_CORE_HEAP];
73
74static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
75 { "otherPss", "otherPrivateDirty", "otherSharedDirty" },
76 { "dalvikPss", "dalvikPrivateDirty", "dalvikSharedDirty" },
77 { "nativePss", "nativePrivateDirty", "nativeSharedDirty" }
78};
79
80jfieldID otherStats_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070083 int pss;
84 int privateDirty;
85 int sharedDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086};
87
88#define BINDER_STATS "/proc/binder/stats"
89
90static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
91{
92#ifdef HAVE_MALLOC_H
93 struct mallinfo info = mallinfo();
94 return (jlong) info.usmblks;
95#else
96 return -1;
97#endif
98}
99
100static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
101{
102#ifdef HAVE_MALLOC_H
103 struct mallinfo info = mallinfo();
104 return (jlong) info.uordblks;
105#else
106 return -1;
107#endif
108}
109
110static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
111{
112#ifdef HAVE_MALLOC_H
113 struct mallinfo info = mallinfo();
114 return (jlong) info.fordblks;
115#else
116 return -1;
117#endif
118}
119
120static void read_mapinfo(FILE *fp, stats_t* stats)
121{
122 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700123 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 bool skip, done = false;
125
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700126 unsigned size = 0, resident = 0, pss = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 unsigned shared_clean = 0, shared_dirty = 0;
128 unsigned private_clean = 0, private_dirty = 0;
129 unsigned referenced = 0;
130 unsigned temp;
131
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700132 unsigned long int start;
133 unsigned long int end = 0;
134 unsigned long int prevEnd = 0;
135 char* name;
136 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700138 int whichHeap = HEAP_UNKNOWN;
139 int prevHeap = HEAP_UNKNOWN;
140
141 if(fgets(line, sizeof(line), fp) == 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
143 while (!done) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700144 prevHeap = whichHeap;
145 prevEnd = end;
146 whichHeap = HEAP_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 skip = false;
148
149 len = strlen(line);
150 if (len < 1) return;
151 line[--len] = 0;
152
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700153 if (sscanf(line, "%lx-%lx %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
154 skip = true;
155 } else {
156 while (isspace(line[name_pos])) {
157 name_pos += 1;
158 }
159 name = line + name_pos;
160 nameLen = strlen(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Ian Rogersafc10e12013-02-22 09:40:58 -0800162 if ((strstr(name, "[heap]") == name) ||
163 (strstr(name, "/dev/ashmem/libc malloc") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700164 whichHeap = HEAP_NATIVE;
165 } else if (strstr(name, "/dev/ashmem/dalvik-") == name) {
166 whichHeap = HEAP_DALVIK;
167 } else if (strstr(name, "/dev/ashmem/CursorWindow") == name) {
168 whichHeap = HEAP_CURSOR;
169 } else if (strstr(name, "/dev/ashmem/") == name) {
170 whichHeap = HEAP_ASHMEM;
171 } else if (strstr(name, "/dev/") == name) {
172 whichHeap = HEAP_UNKNOWN_DEV;
173 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
174 whichHeap = HEAP_SO;
175 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) {
176 whichHeap = HEAP_JAR;
177 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) {
178 whichHeap = HEAP_APK;
179 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) {
180 whichHeap = HEAP_TTF;
Ian Rogers9f8589c2013-02-22 20:35:06 -0800181 } else if ((nameLen > 4 && strcmp(name+nameLen-4, ".dex") == 0) ||
182 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700183 whichHeap = HEAP_DEX;
184 } else if (nameLen > 0) {
185 whichHeap = HEAP_UNKNOWN_MAP;
186 } else if (start == prevEnd && prevHeap == HEAP_SO) {
187 // bss section of a shared library.
188 whichHeap = HEAP_SO;
189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
191
Steve Block6215d3f2012-01-04 20:05:49 +0000192 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 // isSqliteHeap, line);
194
195 while (true) {
196 if (fgets(line, 1024, fp) == 0) {
197 done = true;
198 break;
199 }
200
201 if (sscanf(line, "Size: %d kB", &temp) == 1) {
202 size = temp;
203 } else if (sscanf(line, "Rss: %d kB", &temp) == 1) {
204 resident = temp;
205 } else if (sscanf(line, "Pss: %d kB", &temp) == 1) {
206 pss = temp;
207 } else if (sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
208 shared_clean = temp;
209 } else if (sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
210 shared_dirty = temp;
211 } else if (sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
212 private_clean = temp;
213 } else if (sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
214 private_dirty = temp;
215 } else if (sscanf(line, "Referenced: %d kB", &temp) == 1) {
216 referenced = temp;
Grace Klobabd511162009-07-08 23:32:25 -0700217 } else if (strlen(line) > 30 && line[8] == '-' && line[17] == ' ') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700219 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 break;
221 }
222 }
223
224 if (!skip) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700225 stats[whichHeap].pss += pss;
226 stats[whichHeap].privateDirty += private_dirty;
227 stats[whichHeap].sharedDirty += shared_dirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 }
229 }
230}
231
232static void load_maps(int pid, stats_t* stats)
233{
234 char tmp[128];
235 FILE *fp;
236
237 sprintf(tmp, "/proc/%d/smaps", pid);
238 fp = fopen(tmp, "r");
239 if (fp == 0) return;
240
241 read_mapinfo(fp, stats);
242 fclose(fp);
243}
244
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700245static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
246 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247{
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700248 stats_t stats[_NUM_HEAP];
249 memset(&stats, 0, sizeof(stats));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700251 load_maps(pid, stats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700253 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
254 stats[HEAP_UNKNOWN].pss += stats[i].pss;
255 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
256 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
257 }
258
259 for (int i=0; i<_NUM_CORE_HEAP; i++) {
260 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
261 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
262 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700265 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700267 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
268 if (otherArray == NULL) {
269 return;
270 }
271
272 int j=0;
273 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
274 otherArray[j++] = stats[i].pss;
275 otherArray[j++] = stats[i].privateDirty;
276 otherArray[j++] = stats[i].sharedDirty;
277 }
278
279 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280}
281
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700282static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
283{
284 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
285}
286
Dianne Hackbornb437e092011-08-05 17:50:29 -0700287static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid)
288{
289 char line[1024];
290 jlong pss = 0;
291 unsigned temp;
292
293 char tmp[128];
294 FILE *fp;
295
296 sprintf(tmp, "/proc/%d/smaps", pid);
297 fp = fopen(tmp, "r");
298 if (fp == 0) return 0;
299
300 while (true) {
301 if (fgets(line, 1024, fp) == 0) {
302 break;
303 }
304
305 if (sscanf(line, "Pss: %d kB", &temp) == 1) {
306 pss += temp;
307 }
308 }
309
310 fclose(fp);
311
312 return pss;
313}
314
315static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
316{
317 return android_os_Debug_getPssPid(env, clazz, getpid());
318}
319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320static jint read_binder_stat(const char* stat)
321{
322 FILE* fp = fopen(BINDER_STATS, "r");
323 if (fp == NULL) {
324 return -1;
325 }
326
327 char line[1024];
328
329 char compare[128];
330 int len = snprintf(compare, 128, "proc %d", getpid());
331
332 // loop until we have the block that represents this process
333 do {
334 if (fgets(line, 1024, fp) == 0) {
335 return -1;
336 }
337 } while (strncmp(compare, line, len));
338
339 // now that we have this process, read until we find the stat that we are looking for
340 len = snprintf(compare, 128, " %s: ", stat);
341
342 do {
343 if (fgets(line, 1024, fp) == 0) {
344 return -1;
345 }
346 } while (strncmp(compare, line, len));
347
348 // we have the line, now increment the line ptr to the value
349 char* ptr = line + len;
350 return atoi(ptr);
351}
352
353static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
354{
355 return read_binder_stat("bcTRANSACTION");
356}
357
358static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
359{
360 return read_binder_stat("brTRANSACTION");
361}
362
363// these are implemented in android_util_Binder.cpp
364jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
365jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
366jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
367
Andy McFadden06a6b552010-07-13 16:28:09 -0700368
Andy McFadden06a6b552010-07-13 16:28:09 -0700369/* pulled out of bionic */
370extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
371 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
372extern "C" void free_malloc_leak_info(uint8_t* info);
373#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
374#define BACKTRACE_SIZE 32
375
376/*
377 * This is a qsort() callback.
378 *
379 * See dumpNativeHeap() for comments about the data format and sort order.
380 */
381static int compareHeapRecords(const void* vrec1, const void* vrec2)
382{
383 const size_t* rec1 = (const size_t*) vrec1;
384 const size_t* rec2 = (const size_t*) vrec2;
385 size_t size1 = *rec1;
386 size_t size2 = *rec2;
387
388 if (size1 < size2) {
389 return 1;
390 } else if (size1 > size2) {
391 return -1;
392 }
393
394 intptr_t* bt1 = (intptr_t*)(rec1 + 2);
395 intptr_t* bt2 = (intptr_t*)(rec2 + 2);
396 for (size_t idx = 0; idx < BACKTRACE_SIZE; idx++) {
397 intptr_t addr1 = bt1[idx];
398 intptr_t addr2 = bt2[idx];
399 if (addr1 == addr2) {
400 if (addr1 == 0)
401 break;
402 continue;
403 }
404 if (addr1 < addr2) {
405 return -1;
406 } else if (addr1 > addr2) {
407 return 1;
408 }
409 }
410
411 return 0;
412}
413
414/*
415 * The get_malloc_leak_info() call returns an array of structs that
416 * look like this:
417 *
418 * size_t size
419 * size_t allocations
420 * intptr_t backtrace[32]
421 *
422 * "size" is the size of the allocation, "backtrace" is a fixed-size
423 * array of function pointers, and "allocations" is the number of
424 * allocations with the exact same size and backtrace.
425 *
426 * The entries are sorted by descending total size (i.e. size*allocations)
427 * then allocation count. For best results with "diff" we'd like to sort
428 * primarily by individual size then stack trace. Since the entries are
429 * fixed-size, and we're allowed (by the current implementation) to mangle
430 * them, we can do this in place.
431 */
432static void dumpNativeHeap(FILE* fp)
433{
434 uint8_t* info = NULL;
435 size_t overallSize, infoSize, totalMemory, backtraceSize;
436
437 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory,
438 &backtraceSize);
439 if (info == NULL) {
440 fprintf(fp, "Native heap dump not available. To enable, run these"
441 " commands (requires root):\n");
442 fprintf(fp, "$ adb shell setprop libc.debug.malloc 1\n");
443 fprintf(fp, "$ adb shell stop\n");
444 fprintf(fp, "$ adb shell start\n");
445 return;
446 }
447 assert(infoSize != 0);
448 assert(overallSize % infoSize == 0);
449
450 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
451
452 size_t recordCount = overallSize / infoSize;
453 fprintf(fp, "Total memory: %zu\n", totalMemory);
454 fprintf(fp, "Allocation records: %zd\n", recordCount);
455 if (backtraceSize != BACKTRACE_SIZE) {
456 fprintf(fp, "WARNING: mismatched backtrace sizes (%d vs. %d)\n",
457 backtraceSize, BACKTRACE_SIZE);
458 }
459 fprintf(fp, "\n");
460
461 /* re-sort the entries */
462 qsort(info, recordCount, infoSize, compareHeapRecords);
463
464 /* dump the entries to the file */
465 const uint8_t* ptr = info;
466 for (size_t idx = 0; idx < recordCount; idx++) {
467 size_t size = *(size_t*) ptr;
468 size_t allocations = *(size_t*) (ptr + sizeof(size_t));
469 intptr_t* backtrace = (intptr_t*) (ptr + sizeof(size_t) * 2);
470
471 fprintf(fp, "z %d sz %8zu num %4zu bt",
472 (size & SIZE_FLAG_ZYGOTE_CHILD) != 0,
473 size & ~SIZE_FLAG_ZYGOTE_CHILD,
474 allocations);
475 for (size_t bt = 0; bt < backtraceSize; bt++) {
476 if (backtrace[bt] == 0) {
477 break;
478 } else {
479 fprintf(fp, " %08x", backtrace[bt]);
480 }
481 }
482 fprintf(fp, "\n");
483
484 ptr += infoSize;
485 }
486
Andy McFadden06a6b552010-07-13 16:28:09 -0700487 free_malloc_leak_info(info);
Brian Carlstrom393b84c12010-10-17 23:40:43 -0700488
489 fprintf(fp, "MAPS\n");
490 const char* maps = "/proc/self/maps";
491 FILE* in = fopen(maps, "r");
492 if (in == NULL) {
493 fprintf(fp, "Could not open %s\n", maps);
494 return;
495 }
496 char buf[BUFSIZ];
497 while (size_t n = fread(buf, sizeof(char), BUFSIZ, in)) {
498 fwrite(buf, sizeof(char), n, fp);
499 }
500 fclose(in);
501
502 fprintf(fp, "END\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700503}
Andy McFadden06a6b552010-07-13 16:28:09 -0700504
505/*
506 * Dump the native heap, writing human-readable output to the specified
507 * file descriptor.
508 */
509static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz,
510 jobject fileDescriptor)
511{
512 if (fileDescriptor == NULL) {
513 jniThrowNullPointerException(env, NULL);
514 return;
515 }
516 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
517 if (origFd < 0) {
518 jniThrowRuntimeException(env, "Invalid file descriptor");
519 return;
520 }
521
522 /* dup() the descriptor so we don't close the original with fclose() */
523 int fd = dup(origFd);
524 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000525 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700526 jniThrowRuntimeException(env, "dup() failed");
527 return;
528 }
529
530 FILE* fp = fdopen(fd, "w");
531 if (fp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000532 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700533 close(fd);
534 jniThrowRuntimeException(env, "fdopen() failed");
535 return;
536 }
537
Steve Block5baa3a62011-12-20 16:23:08 +0000538 ALOGD("Native heap dump starting...\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700539 dumpNativeHeap(fp);
Steve Block5baa3a62011-12-20 16:23:08 +0000540 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700541
542 fclose(fp);
543}
544
545
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700546static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz,
547 jint pid, jstring fileName)
548{
549 if (fileName == NULL) {
550 jniThrowNullPointerException(env, NULL);
551 return;
552 }
553 const jchar* str = env->GetStringCritical(fileName, 0);
554 String8 fileName8;
555 if (str) {
556 fileName8 = String8(str, env->GetStringLength(fileName));
557 env->ReleaseStringCritical(fileName, str);
558 }
559
560 int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
561 if (fd < 0) {
562 fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno));
563 return;
564 }
565
566 if (lseek(fd, 0, SEEK_END) < 0) {
567 fprintf(stderr, "lseek: %s\n", strerror(errno));
568 } else {
569 dump_backtrace_to_file(pid, fd);
570 }
571
572 close(fd);
573}
574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575/*
576 * JNI registration.
577 */
578
579static JNINativeMethod gMethods[] = {
580 { "getNativeHeapSize", "()J",
581 (void*) android_os_Debug_getNativeHeapSize },
582 { "getNativeHeapAllocatedSize", "()J",
583 (void*) android_os_Debug_getNativeHeapAllocatedSize },
584 { "getNativeHeapFreeSize", "()J",
585 (void*) android_os_Debug_getNativeHeapFreeSize },
586 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
587 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700588 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
589 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -0700590 { "getPss", "()J",
591 (void*) android_os_Debug_getPss },
592 { "getPss", "(I)J",
593 (void*) android_os_Debug_getPssPid },
Andy McFadden06a6b552010-07-13 16:28:09 -0700594 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
595 (void*) android_os_Debug_dumpNativeHeap },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 { "getBinderSentTransactions", "()I",
597 (void*) android_os_Debug_getBinderSentTransactions },
598 { "getBinderReceivedTransactions", "()I",
599 (void*) android_os_getBinderReceivedTransactions },
600 { "getBinderLocalObjectCount", "()I",
601 (void*)android_os_Debug_getLocalObjectCount },
602 { "getBinderProxyObjectCount", "()I",
603 (void*)android_os_Debug_getProxyObjectCount },
604 { "getBinderDeathObjectCount", "()I",
605 (void*)android_os_Debug_getDeathObjectCount },
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700606 { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V",
607 (void*)android_os_Debug_dumpNativeBacktraceToFile },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608};
609
610int register_android_os_Debug(JNIEnv *env)
611{
612 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700614 for (int i=0; i<_NUM_CORE_HEAP; i++) {
615 stat_fields[i].pss_field =
616 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
617 stat_fields[i].privateDirty_field =
618 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
619 stat_fields[i].sharedDirty_field =
620 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
621 }
622
623 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
626}
627
Andy McFadden06a6b552010-07-13 16:28:09 -0700628}; // namespace android