Debug.java revision 2970c499388b4dcd1232cd622a9b80b395eeb2b4
1/* 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 17package android.os; 18 19import com.android.internal.util.TypedProperties; 20 21import android.util.Config; 22import android.util.Log; 23 24import java.io.FileDescriptor; 25import java.io.FileNotFoundException; 26import java.io.FileOutputStream; 27import java.io.FileReader; 28import java.io.IOException; 29import java.io.OutputStreamWriter; 30import java.io.PrintWriter; 31import java.io.Reader; 32import java.lang.reflect.Field; 33import java.lang.reflect.Modifier; 34import java.lang.annotation.Target; 35import java.lang.annotation.ElementType; 36import java.lang.annotation.Retention; 37import java.lang.annotation.RetentionPolicy; 38 39import org.apache.harmony.dalvik.ddmc.Chunk; 40import org.apache.harmony.dalvik.ddmc.ChunkHandler; 41import org.apache.harmony.dalvik.ddmc.DdmServer; 42 43import dalvik.bytecode.Opcodes; 44import dalvik.system.VMDebug; 45 46 47/** 48 * Provides various debugging functions for Android applications, including 49 * tracing and allocation counts. 50 * <p><strong>Logging Trace Files</strong></p> 51 * <p>Debug can create log files that give details about an application, such as 52 * a call stack and start/stop times for any running methods. See <a 53href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for 54 * information about reading trace files. To start logging trace files, call one 55 * of the startMethodTracing() methods. To stop tracing, call 56 * {@link #stopMethodTracing()}. 57 */ 58public final class Debug 59{ 60 private static final String TAG = "Debug"; 61 62 /** 63 * Flags for startMethodTracing(). These can be ORed together. 64 * 65 * TRACE_COUNT_ALLOCS adds the results from startAllocCounting to the 66 * trace key file. 67 */ 68 public static final int TRACE_COUNT_ALLOCS = VMDebug.TRACE_COUNT_ALLOCS; 69 70 /** 71 * Flags for printLoadedClasses(). Default behavior is to only show 72 * the class name. 73 */ 74 public static final int SHOW_FULL_DETAIL = 1; 75 public static final int SHOW_CLASSLOADER = (1 << 1); 76 public static final int SHOW_INITIALIZED = (1 << 2); 77 78 // set/cleared by waitForDebugger() 79 private static volatile boolean mWaiting = false; 80 81 private Debug() {} 82 83 /* 84 * How long to wait for the debugger to finish sending requests. I've 85 * seen this hit 800msec on the device while waiting for a response 86 * to travel over USB and get processed, so we take that and add 87 * half a second. 88 */ 89 private static final int MIN_DEBUGGER_IDLE = 1300; // msec 90 91 /* how long to sleep when polling for activity */ 92 private static final int SPIN_DELAY = 200; // msec 93 94 /** 95 * Default trace file path and file 96 */ 97 private static final String DEFAULT_TRACE_PATH_PREFIX = 98 Environment.getExternalStorageDirectory().getPath() + "/"; 99 private static final String DEFAULT_TRACE_BODY = "dmtrace"; 100 private static final String DEFAULT_TRACE_EXTENSION = ".trace"; 101 private static final String DEFAULT_TRACE_FILE_PATH = 102 DEFAULT_TRACE_PATH_PREFIX + DEFAULT_TRACE_BODY 103 + DEFAULT_TRACE_EXTENSION; 104 105 106 /** 107 * This class is used to retrieved various statistics about the memory mappings for this 108 * process. The returns info broken down by dalvik, native, and other. All results are in kB. 109 */ 110 public static class MemoryInfo implements Parcelable { 111 /** The proportional set size for dalvik. */ 112 public int dalvikPss; 113 /** The private dirty pages used by dalvik. */ 114 public int dalvikPrivateDirty; 115 /** The shared dirty pages used by dalvik. */ 116 public int dalvikSharedDirty; 117 118 /** The proportional set size for the native heap. */ 119 public int nativePss; 120 /** The private dirty pages used by the native heap. */ 121 public int nativePrivateDirty; 122 /** The shared dirty pages used by the native heap. */ 123 public int nativeSharedDirty; 124 125 /** The proportional set size for everything else. */ 126 public int otherPss; 127 /** The private dirty pages used by everything else. */ 128 public int otherPrivateDirty; 129 /** The shared dirty pages used by everything else. */ 130 public int otherSharedDirty; 131 132 public MemoryInfo() { 133 } 134 135 /** 136 * Return total PSS memory usage in kB. 137 */ 138 public int getTotalPss() { 139 return dalvikPss + nativePss + otherPss; 140 } 141 142 /** 143 * Return total private dirty memory usage in kB. 144 */ 145 public int getTotalPrivateDirty() { 146 return dalvikPrivateDirty + nativePrivateDirty + otherPrivateDirty; 147 } 148 149 /** 150 * Return total shared dirty memory usage in kB. 151 */ 152 public int getTotalSharedDirty() { 153 return dalvikSharedDirty + nativeSharedDirty + otherSharedDirty; 154 } 155 156 public int describeContents() { 157 return 0; 158 } 159 160 public void writeToParcel(Parcel dest, int flags) { 161 dest.writeInt(dalvikPss); 162 dest.writeInt(dalvikPrivateDirty); 163 dest.writeInt(dalvikSharedDirty); 164 dest.writeInt(nativePss); 165 dest.writeInt(nativePrivateDirty); 166 dest.writeInt(nativeSharedDirty); 167 dest.writeInt(otherPss); 168 dest.writeInt(otherPrivateDirty); 169 dest.writeInt(otherSharedDirty); 170 } 171 172 public void readFromParcel(Parcel source) { 173 dalvikPss = source.readInt(); 174 dalvikPrivateDirty = source.readInt(); 175 dalvikSharedDirty = source.readInt(); 176 nativePss = source.readInt(); 177 nativePrivateDirty = source.readInt(); 178 nativeSharedDirty = source.readInt(); 179 otherPss = source.readInt(); 180 otherPrivateDirty = source.readInt(); 181 otherSharedDirty = source.readInt(); 182 } 183 184 public static final Creator<MemoryInfo> CREATOR = new Creator<MemoryInfo>() { 185 public MemoryInfo createFromParcel(Parcel source) { 186 return new MemoryInfo(source); 187 } 188 public MemoryInfo[] newArray(int size) { 189 return new MemoryInfo[size]; 190 } 191 }; 192 193 private MemoryInfo(Parcel source) { 194 readFromParcel(source); 195 } 196 } 197 198 199 /** 200 * Wait until a debugger attaches. As soon as the debugger attaches, 201 * this returns, so you will need to place a breakpoint after the 202 * waitForDebugger() call if you want to start tracing immediately. 203 */ 204 public static void waitForDebugger() { 205 if (!VMDebug.isDebuggingEnabled()) { 206 //System.out.println("debugging not enabled, not waiting"); 207 return; 208 } 209 if (isDebuggerConnected()) 210 return; 211 212 // if DDMS is listening, inform them of our plight 213 System.out.println("Sending WAIT chunk"); 214 byte[] data = new byte[] { 0 }; // 0 == "waiting for debugger" 215 Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1); 216 DdmServer.sendChunk(waitChunk); 217 218 mWaiting = true; 219 while (!isDebuggerConnected()) { 220 try { Thread.sleep(SPIN_DELAY); } 221 catch (InterruptedException ie) {} 222 } 223 mWaiting = false; 224 225 System.out.println("Debugger has connected"); 226 227 /* 228 * There is no "ready to go" signal from the debugger, and we're 229 * not allowed to suspend ourselves -- the debugger expects us to 230 * be running happily, and gets confused if we aren't. We need to 231 * allow the debugger a chance to set breakpoints before we start 232 * running again. 233 * 234 * Sit and spin until the debugger has been idle for a short while. 235 */ 236 while (true) { 237 long delta = VMDebug.lastDebuggerActivity(); 238 if (delta < 0) { 239 System.out.println("debugger detached?"); 240 break; 241 } 242 243 if (delta < MIN_DEBUGGER_IDLE) { 244 System.out.println("waiting for debugger to settle..."); 245 try { Thread.sleep(SPIN_DELAY); } 246 catch (InterruptedException ie) {} 247 } else { 248 System.out.println("debugger has settled (" + delta + ")"); 249 break; 250 } 251 } 252 } 253 254 /** 255 * Returns "true" if one or more threads is waiting for a debugger 256 * to attach. 257 */ 258 public static boolean waitingForDebugger() { 259 return mWaiting; 260 } 261 262 /** 263 * Determine if a debugger is currently attached. 264 */ 265 public static boolean isDebuggerConnected() { 266 return VMDebug.isDebuggerConnected(); 267 } 268 269 /** 270 * Returns an array of strings that identify VM features. This is 271 * used by DDMS to determine what sorts of operations the VM can 272 * perform. 273 * 274 * @hide 275 */ 276 public static String[] getVmFeatureList() { 277 return VMDebug.getVmFeatureList(); 278 } 279 280 /** 281 * Change the JDWP port. 282 * 283 * @deprecated no longer needed or useful 284 */ 285 @Deprecated 286 public static void changeDebugPort(int port) {} 287 288 /** 289 * This is the pathname to the sysfs file that enables and disables 290 * tracing on the qemu emulator. 291 */ 292 private static final String SYSFS_QEMU_TRACE_STATE = "/sys/qemu_trace/state"; 293 294 /** 295 * Enable qemu tracing. For this to work requires running everything inside 296 * the qemu emulator; otherwise, this method will have no effect. The trace 297 * file is specified on the command line when the emulator is started. For 298 * example, the following command line <br /> 299 * <code>emulator -trace foo</code><br /> 300 * will start running the emulator and create a trace file named "foo". This 301 * method simply enables writing the trace records to the trace file. 302 * 303 * <p> 304 * The main differences between this and {@link #startMethodTracing()} are 305 * that tracing in the qemu emulator traces every cpu instruction of every 306 * process, including kernel code, so we have more complete information, 307 * including all context switches. We can also get more detailed information 308 * such as cache misses. The sequence of calls is determined by 309 * post-processing the instruction trace. The qemu tracing is also done 310 * without modifying the application or perturbing the timing of calls 311 * because no instrumentation is added to the application being traced. 312 * </p> 313 * 314 * <p> 315 * One limitation of using this method compared to using 316 * {@link #startMethodTracing()} on the real device is that the emulator 317 * does not model all of the real hardware effects such as memory and 318 * bus contention. The emulator also has a simple cache model and cannot 319 * capture all the complexities of a real cache. 320 * </p> 321 */ 322 public static void startNativeTracing() { 323 // Open the sysfs file for writing and write "1" to it. 324 PrintWriter outStream = null; 325 try { 326 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE); 327 outStream = new PrintWriter(new OutputStreamWriter(fos)); 328 outStream.println("1"); 329 } catch (Exception e) { 330 } finally { 331 if (outStream != null) 332 outStream.close(); 333 } 334 335 VMDebug.startEmulatorTracing(); 336 } 337 338 /** 339 * Stop qemu tracing. See {@link #startNativeTracing()} to start tracing. 340 * 341 * <p>Tracing can be started and stopped as many times as desired. When 342 * the qemu emulator itself is stopped then the buffered trace records 343 * are flushed and written to the trace file. In fact, it is not necessary 344 * to call this method at all; simply killing qemu is sufficient. But 345 * starting and stopping a trace is useful for examining a specific 346 * region of code.</p> 347 */ 348 public static void stopNativeTracing() { 349 VMDebug.stopEmulatorTracing(); 350 351 // Open the sysfs file for writing and write "0" to it. 352 PrintWriter outStream = null; 353 try { 354 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE); 355 outStream = new PrintWriter(new OutputStreamWriter(fos)); 356 outStream.println("0"); 357 } catch (Exception e) { 358 // We could print an error message here but we probably want 359 // to quietly ignore errors if we are not running in the emulator. 360 } finally { 361 if (outStream != null) 362 outStream.close(); 363 } 364 } 365 366 /** 367 * Enable "emulator traces", in which information about the current 368 * method is made available to the "emulator -trace" feature. There 369 * is no corresponding "disable" call -- this is intended for use by 370 * the framework when tracing should be turned on and left that way, so 371 * that traces captured with F9/F10 will include the necessary data. 372 * 373 * This puts the VM into "profile" mode, which has performance 374 * consequences. 375 * 376 * To temporarily enable tracing, use {@link #startNativeTracing()}. 377 */ 378 public static void enableEmulatorTraceOutput() { 379 VMDebug.startEmulatorTracing(); 380 } 381 382 /** 383 * Start method tracing with default log name and buffer size. See <a 384href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for 385 * information about reading these files. Call stopMethodTracing() to stop 386 * tracing. 387 */ 388 public static void startMethodTracing() { 389 VMDebug.startMethodTracing(DEFAULT_TRACE_FILE_PATH, 0, 0); 390 } 391 392 /** 393 * Start method tracing, specifying the trace log file name. The trace 394 * file will be put under "/sdcard" unless an absolute path is given. 395 * See <a 396 href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for 397 * information about reading trace files. 398 * 399 * @param traceName Name for the trace log file to create. 400 * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". 401 * If the files already exist, they will be truncated. 402 * If the trace file given does not end in ".trace", it will be appended for you. 403 */ 404 public static void startMethodTracing(String traceName) { 405 startMethodTracing(traceName, 0, 0); 406 } 407 408 /** 409 * Start method tracing, specifying the trace log file name and the 410 * buffer size. The trace files will be put under "/sdcard" unless an 411 * absolute path is given. See <a 412 href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for 413 * information about reading trace files. 414 * @param traceName Name for the trace log file to create. 415 * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". 416 * If the files already exist, they will be truncated. 417 * If the trace file given does not end in ".trace", it will be appended for you. 418 * 419 * @param bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB. 420 */ 421 public static void startMethodTracing(String traceName, int bufferSize) { 422 startMethodTracing(traceName, bufferSize, 0); 423 } 424 425 /** 426 * Start method tracing, specifying the trace log file name and the 427 * buffer size. The trace files will be put under "/sdcard" unless an 428 * absolute path is given. See <a 429 href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for 430 * information about reading trace files. 431 * 432 * <p> 433 * When method tracing is enabled, the VM will run more slowly than 434 * usual, so the timings from the trace files should only be considered 435 * in relative terms (e.g. was run #1 faster than run #2). The times 436 * for native methods will not change, so don't try to use this to 437 * compare the performance of interpreted and native implementations of the 438 * same method. As an alternative, consider using "native" tracing 439 * in the emulator via {@link #startNativeTracing()}. 440 * </p> 441 * 442 * @param traceName Name for the trace log file to create. 443 * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". 444 * If the files already exist, they will be truncated. 445 * If the trace file given does not end in ".trace", it will be appended for you. 446 * @param bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB. 447 */ 448 public static void startMethodTracing(String traceName, int bufferSize, 449 int flags) { 450 451 String pathName = traceName; 452 if (pathName.charAt(0) != '/') 453 pathName = DEFAULT_TRACE_PATH_PREFIX + pathName; 454 if (!pathName.endsWith(DEFAULT_TRACE_EXTENSION)) 455 pathName = pathName + DEFAULT_TRACE_EXTENSION; 456 457 VMDebug.startMethodTracing(pathName, bufferSize, flags); 458 } 459 460 /** 461 * Like startMethodTracing(String, int, int), but taking an already-opened 462 * FileDescriptor in which the trace is written. The file name is also 463 * supplied simply for logging. Makes a dup of the file descriptor. 464 * 465 * Not exposed in the SDK unless we are really comfortable with supporting 466 * this and find it would be useful. 467 * @hide 468 */ 469 public static void startMethodTracing(String traceName, FileDescriptor fd, 470 int bufferSize, int flags) { 471 VMDebug.startMethodTracing(traceName, fd, bufferSize, flags); 472 } 473 474 /** 475 * Starts method tracing without a backing file. When stopMethodTracing 476 * is called, the result is sent directly to DDMS. (If DDMS is not 477 * attached when tracing ends, the profiling data will be discarded.) 478 * 479 * @hide 480 */ 481 public static void startMethodTracingDdms(int bufferSize, int flags) { 482 VMDebug.startMethodTracingDdms(bufferSize, flags); 483 } 484 485 /** 486 * Determine whether method tracing is currently active. 487 * @hide 488 */ 489 public static boolean isMethodTracingActive() { 490 return VMDebug.isMethodTracingActive(); 491 } 492 493 /** 494 * Stop method tracing. 495 */ 496 public static void stopMethodTracing() { 497 VMDebug.stopMethodTracing(); 498 } 499 500 /** 501 * Get an indication of thread CPU usage. The value returned 502 * indicates the amount of time that the current thread has spent 503 * executing code or waiting for certain types of I/O. 504 * 505 * The time is expressed in nanoseconds, and is only meaningful 506 * when compared to the result from an earlier call. Note that 507 * nanosecond resolution does not imply nanosecond accuracy. 508 * 509 * On system which don't support this operation, the call returns -1. 510 */ 511 public static long threadCpuTimeNanos() { 512 return VMDebug.threadCpuTimeNanos(); 513 } 514 515 /** 516 * Start counting the number and aggregate size of memory allocations. 517 * 518 * <p>The {@link #startAllocCounting() start} function resets the counts and enables counting. 519 * The {@link #stopAllocCounting() stop} function disables the counting so that the analysis 520 * code doesn't cause additional allocations. The various <code>get</code> functions return 521 * the specified value. And the various <code>reset</code> functions reset the specified 522 * count.</p> 523 * 524 * <p>Counts are kept for the system as a whole and for each thread. 525 * The per-thread counts for threads other than the current thread 526 * are not cleared by the "reset" or "start" calls.</p> 527 */ 528 public static void startAllocCounting() { 529 VMDebug.startAllocCounting(); 530 } 531 532 /** 533 * Stop counting the number and aggregate size of memory allocations. 534 * 535 * @see #startAllocCounting() 536 */ 537 public static void stopAllocCounting() { 538 VMDebug.stopAllocCounting(); 539 } 540 541 public static int getGlobalAllocCount() { 542 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS); 543 } 544 public static int getGlobalAllocSize() { 545 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES); 546 } 547 public static int getGlobalFreedCount() { 548 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS); 549 } 550 public static int getGlobalFreedSize() { 551 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES); 552 } 553 public static int getGlobalClassInitCount() { 554 /* number of classes that have been successfully initialized */ 555 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT); 556 } 557 public static int getGlobalClassInitTime() { 558 /* cumulative elapsed time for class initialization, in usec */ 559 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME); 560 } 561 public static int getGlobalExternalAllocCount() { 562 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_OBJECTS); 563 } 564 public static int getGlobalExternalAllocSize() { 565 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_BYTES); 566 } 567 public static int getGlobalExternalFreedCount() { 568 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_OBJECTS); 569 } 570 public static int getGlobalExternalFreedSize() { 571 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_BYTES); 572 } 573 public static int getGlobalGcInvocationCount() { 574 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS); 575 } 576 public static int getThreadAllocCount() { 577 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS); 578 } 579 public static int getThreadAllocSize() { 580 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES); 581 } 582 public static int getThreadExternalAllocCount() { 583 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_OBJECTS); 584 } 585 public static int getThreadExternalAllocSize() { 586 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_BYTES); 587 } 588 public static int getThreadGcInvocationCount() { 589 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS); 590 } 591 592 public static void resetGlobalAllocCount() { 593 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS); 594 } 595 public static void resetGlobalAllocSize() { 596 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES); 597 } 598 public static void resetGlobalFreedCount() { 599 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS); 600 } 601 public static void resetGlobalFreedSize() { 602 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES); 603 } 604 public static void resetGlobalClassInitCount() { 605 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT); 606 } 607 public static void resetGlobalClassInitTime() { 608 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME); 609 } 610 public static void resetGlobalExternalAllocCount() { 611 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_OBJECTS); 612 } 613 public static void resetGlobalExternalAllocSize() { 614 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_BYTES); 615 } 616 public static void resetGlobalExternalFreedCount() { 617 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_OBJECTS); 618 } 619 public static void resetGlobalExternalFreedSize() { 620 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_BYTES); 621 } 622 public static void resetGlobalGcInvocationCount() { 623 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS); 624 } 625 public static void resetThreadAllocCount() { 626 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS); 627 } 628 public static void resetThreadAllocSize() { 629 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES); 630 } 631 public static void resetThreadExternalAllocCount() { 632 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_OBJECTS); 633 } 634 public static void resetThreadExternalAllocSize() { 635 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_BYTES); 636 } 637 public static void resetThreadGcInvocationCount() { 638 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS); 639 } 640 public static void resetAllCounts() { 641 VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS); 642 } 643 644 /** 645 * Returns the size of the native heap. 646 * @return The size of the native heap in bytes. 647 */ 648 public static native long getNativeHeapSize(); 649 650 /** 651 * Returns the amount of allocated memory in the native heap. 652 * @return The allocated size in bytes. 653 */ 654 public static native long getNativeHeapAllocatedSize(); 655 656 /** 657 * Returns the amount of free memory in the native heap. 658 * @return The freed size in bytes. 659 */ 660 public static native long getNativeHeapFreeSize(); 661 662 /** 663 * Retrieves information about this processes memory usages. This information is broken down by 664 * how much is in use by dalivk, the native heap, and everything else. 665 */ 666 public static native void getMemoryInfo(MemoryInfo memoryInfo); 667 668 /** 669 * Note: currently only works when the requested pid has the same UID 670 * as the caller. 671 * @hide 672 */ 673 public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo); 674 675 /** 676 * Establish an object allocation limit in the current thread. Useful 677 * for catching regressions in code that is expected to operate 678 * without causing any allocations. 679 * 680 * <p>Pass in the maximum number of allowed allocations. Use -1 to disable 681 * the limit. Returns the previous limit.</p> 682 * 683 * <p>The preferred way to use this is: 684 * <pre> 685 * int prevLimit = -1; 686 * try { 687 * prevLimit = Debug.setAllocationLimit(0); 688 * ... do stuff that's not expected to allocate memory ... 689 * } finally { 690 * Debug.setAllocationLimit(prevLimit); 691 * } 692 * </pre> 693 * This allows limits to be nested. The try/finally ensures that the 694 * limit is reset if something fails.</p> 695 * 696 * <p>Exceeding the limit causes a dalvik.system.AllocationLimitError to 697 * be thrown from a memory allocation call. The limit is reset to -1 698 * when this happens.</p> 699 * 700 * <p>The feature may be disabled in the VM configuration. If so, this 701 * call has no effect, and always returns -1.</p> 702 */ 703 public static int setAllocationLimit(int limit) { 704 return VMDebug.setAllocationLimit(limit); 705 } 706 707 /** 708 * Establish a global object allocation limit. This is similar to 709 * {@link #setAllocationLimit(int)} but applies to all threads in 710 * the VM. It will coexist peacefully with per-thread limits. 711 * 712 * [ The value of "limit" is currently restricted to 0 (no allocations 713 * allowed) or -1 (no global limit). This may be changed in a future 714 * release. ] 715 */ 716 public static int setGlobalAllocationLimit(int limit) { 717 if (limit != 0 && limit != -1) 718 throw new IllegalArgumentException("limit must be 0 or -1"); 719 return VMDebug.setGlobalAllocationLimit(limit); 720 } 721 722 /** 723 * Dump a list of all currently loaded class to the log file. 724 * 725 * @param flags See constants above. 726 */ 727 public static void printLoadedClasses(int flags) { 728 VMDebug.printLoadedClasses(flags); 729 } 730 731 /** 732 * Get the number of loaded classes. 733 * @return the number of loaded classes. 734 */ 735 public static int getLoadedClassCount() { 736 return VMDebug.getLoadedClassCount(); 737 } 738 739 /** 740 * Dump "hprof" data to the specified file. This may cause a GC. 741 * 742 * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof"). 743 * @throws UnsupportedOperationException if the VM was built without 744 * HPROF support. 745 * @throws IOException if an error occurs while opening or writing files. 746 */ 747 public static void dumpHprofData(String fileName) throws IOException { 748 VMDebug.dumpHprofData(fileName); 749 } 750 751 /** 752 * Like dumpHprofData(String), but takes an already-opened 753 * FileDescriptor to which the trace is written. The file name is also 754 * supplied simply for logging. Makes a dup of the file descriptor. 755 * 756 * Primarily for use by the "am" shell command. 757 * 758 * @hide 759 */ 760 public static void dumpHprofData(String fileName, FileDescriptor fd) 761 throws IOException { 762 VMDebug.dumpHprofData(fileName, fd); 763 } 764 765 /** 766 * Collect "hprof" and send it to DDMS. This may cause a GC. 767 * 768 * @throws UnsupportedOperationException if the VM was built without 769 * HPROF support. 770 * @hide 771 */ 772 public static void dumpHprofDataDdms() { 773 VMDebug.dumpHprofDataDdms(); 774 } 775 776 /** 777 * Writes native heap data to the specified file descriptor. 778 * 779 * @hide 780 */ 781 public static native void dumpNativeHeap(FileDescriptor fd); 782 783 /** 784 * Returns a count of the extant instances of a class. 785 * 786 * @hide 787 */ 788 public static long countInstancesOfClass(Class cls) { 789 return VMDebug.countInstancesOfClass(cls); 790 } 791 792 /** 793 * Returns the number of sent transactions from this process. 794 * @return The number of sent transactions or -1 if it could not read t. 795 */ 796 public static native int getBinderSentTransactions(); 797 798 /** 799 * Returns the number of received transactions from the binder driver. 800 * @return The number of received transactions or -1 if it could not read the stats. 801 */ 802 public static native int getBinderReceivedTransactions(); 803 804 /** 805 * Returns the number of active local Binder objects that exist in the 806 * current process. 807 */ 808 public static final native int getBinderLocalObjectCount(); 809 810 /** 811 * Returns the number of references to remote proxy Binder objects that 812 * exist in the current process. 813 */ 814 public static final native int getBinderProxyObjectCount(); 815 816 /** 817 * Returns the number of death notification links to Binder objects that 818 * exist in the current process. 819 */ 820 public static final native int getBinderDeathObjectCount(); 821 822 /** 823 * Primes the register map cache. 824 * 825 * Only works for classes in the bootstrap class loader. Does not 826 * cause classes to be loaded if they're not already present. 827 * 828 * The classAndMethodDesc argument is a concatentation of the VM-internal 829 * class descriptor, method name, and method descriptor. Examples: 830 * Landroid/os/Looper;.loop:()V 831 * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V 832 * 833 * @param classAndMethodDesc the method to prepare 834 * 835 * @hide 836 */ 837 public static final boolean cacheRegisterMap(String classAndMethodDesc) { 838 return VMDebug.cacheRegisterMap(classAndMethodDesc); 839 } 840 841 /** 842 * Dumps the contents of VM reference tables (e.g. JNI locals and 843 * globals) to the log file. 844 * 845 * @hide 846 */ 847 public static final void dumpReferenceTables() { 848 VMDebug.dumpReferenceTables(); 849 } 850 851 /** 852 * API for gathering and querying instruction counts. 853 * 854 * Example usage: 855 * <pre> 856 * Debug.InstructionCount icount = new Debug.InstructionCount(); 857 * icount.resetAndStart(); 858 * [... do lots of stuff ...] 859 * if (icount.collect()) { 860 * System.out.println("Total instructions executed: " 861 * + icount.globalTotal()); 862 * System.out.println("Method invocations: " 863 * + icount.globalMethodInvocations()); 864 * } 865 * </pre> 866 */ 867 public static class InstructionCount { 868 private static final int NUM_INSTR = 256; 869 870 private int[] mCounts; 871 872 public InstructionCount() { 873 mCounts = new int[NUM_INSTR]; 874 } 875 876 /** 877 * Reset counters and ensure counts are running. Counts may 878 * have already been running. 879 * 880 * @return true if counting was started 881 */ 882 public boolean resetAndStart() { 883 try { 884 VMDebug.startInstructionCounting(); 885 VMDebug.resetInstructionCount(); 886 } catch (UnsupportedOperationException uoe) { 887 return false; 888 } 889 return true; 890 } 891 892 /** 893 * Collect instruction counts. May or may not stop the 894 * counting process. 895 */ 896 public boolean collect() { 897 try { 898 VMDebug.stopInstructionCounting(); 899 VMDebug.getInstructionCount(mCounts); 900 } catch (UnsupportedOperationException uoe) { 901 return false; 902 } 903 return true; 904 } 905 906 /** 907 * Return the total number of instructions executed globally (i.e. in 908 * all threads). 909 */ 910 public int globalTotal() { 911 int count = 0; 912 for (int i = 0; i < NUM_INSTR; i++) 913 count += mCounts[i]; 914 return count; 915 } 916 917 /** 918 * Return the total number of method-invocation instructions 919 * executed globally. 920 */ 921 public int globalMethodInvocations() { 922 int count = 0; 923 924 //count += mCounts[Opcodes.OP_EXECUTE_INLINE]; 925 count += mCounts[Opcodes.OP_INVOKE_VIRTUAL]; 926 count += mCounts[Opcodes.OP_INVOKE_SUPER]; 927 count += mCounts[Opcodes.OP_INVOKE_DIRECT]; 928 count += mCounts[Opcodes.OP_INVOKE_STATIC]; 929 count += mCounts[Opcodes.OP_INVOKE_INTERFACE]; 930 count += mCounts[Opcodes.OP_INVOKE_VIRTUAL_RANGE]; 931 count += mCounts[Opcodes.OP_INVOKE_SUPER_RANGE]; 932 count += mCounts[Opcodes.OP_INVOKE_DIRECT_RANGE]; 933 count += mCounts[Opcodes.OP_INVOKE_STATIC_RANGE]; 934 count += mCounts[Opcodes.OP_INVOKE_INTERFACE_RANGE]; 935 //count += mCounts[Opcodes.OP_INVOKE_DIRECT_EMPTY]; 936 count += mCounts[Opcodes.OP_INVOKE_VIRTUAL_QUICK]; 937 count += mCounts[Opcodes.OP_INVOKE_VIRTUAL_QUICK_RANGE]; 938 count += mCounts[Opcodes.OP_INVOKE_SUPER_QUICK]; 939 count += mCounts[Opcodes.OP_INVOKE_SUPER_QUICK_RANGE]; 940 return count; 941 } 942 } 943 944 945 /** 946 * A Map of typed debug properties. 947 */ 948 private static final TypedProperties debugProperties; 949 950 /* 951 * Load the debug properties from the standard files into debugProperties. 952 */ 953 static { 954 if (Config.DEBUG) { 955 final String TAG = "DebugProperties"; 956 final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" }; 957 final TypedProperties tp = new TypedProperties(); 958 959 // Read the properties from each of the files, if present. 960 for (String file : files) { 961 Reader r; 962 try { 963 r = new FileReader(file); 964 } catch (FileNotFoundException ex) { 965 // It's ok if a file is missing. 966 continue; 967 } 968 969 try { 970 tp.load(r); 971 } catch (Exception ex) { 972 throw new RuntimeException("Problem loading " + file, ex); 973 } finally { 974 try { 975 r.close(); 976 } catch (IOException ex) { 977 // Ignore this error. 978 } 979 } 980 } 981 982 debugProperties = tp.isEmpty() ? null : tp; 983 } else { 984 debugProperties = null; 985 } 986 } 987 988 989 /** 990 * Returns true if the type of the field matches the specified class. 991 * Handles the case where the class is, e.g., java.lang.Boolean, but 992 * the field is of the primitive "boolean" type. Also handles all of 993 * the java.lang.Number subclasses. 994 */ 995 private static boolean fieldTypeMatches(Field field, Class<?> cl) { 996 Class<?> fieldClass = field.getType(); 997 if (fieldClass == cl) { 998 return true; 999 } 1000 Field primitiveTypeField; 1001 try { 1002 /* All of the classes we care about (Boolean, Integer, etc.) 1003 * have a Class field called "TYPE" that points to the corresponding 1004 * primitive class. 1005 */ 1006 primitiveTypeField = cl.getField("TYPE"); 1007 } catch (NoSuchFieldException ex) { 1008 return false; 1009 } 1010 try { 1011 return fieldClass == (Class<?>) primitiveTypeField.get(null); 1012 } catch (IllegalAccessException ex) { 1013 return false; 1014 } 1015 } 1016 1017 1018 /** 1019 * Looks up the property that corresponds to the field, and sets the field's value 1020 * if the types match. 1021 */ 1022 private static void modifyFieldIfSet(final Field field, final TypedProperties properties, 1023 final String propertyName) { 1024 if (field.getType() == java.lang.String.class) { 1025 int stringInfo = properties.getStringInfo(propertyName); 1026 switch (stringInfo) { 1027 case TypedProperties.STRING_SET: 1028 // Handle as usual below. 1029 break; 1030 case TypedProperties.STRING_NULL: 1031 try { 1032 field.set(null, null); // null object for static fields; null string 1033 } catch (IllegalAccessException ex) { 1034 throw new IllegalArgumentException( 1035 "Cannot set field for " + propertyName, ex); 1036 } 1037 return; 1038 case TypedProperties.STRING_NOT_SET: 1039 return; 1040 case TypedProperties.STRING_TYPE_MISMATCH: 1041 throw new IllegalArgumentException( 1042 "Type of " + propertyName + " " + 1043 " does not match field type (" + field.getType() + ")"); 1044 default: 1045 throw new IllegalStateException( 1046 "Unexpected getStringInfo(" + propertyName + ") return value " + 1047 stringInfo); 1048 } 1049 } 1050 Object value = properties.get(propertyName); 1051 if (value != null) { 1052 if (!fieldTypeMatches(field, value.getClass())) { 1053 throw new IllegalArgumentException( 1054 "Type of " + propertyName + " (" + value.getClass() + ") " + 1055 " does not match field type (" + field.getType() + ")"); 1056 } 1057 try { 1058 field.set(null, value); // null object for static fields 1059 } catch (IllegalAccessException ex) { 1060 throw new IllegalArgumentException( 1061 "Cannot set field for " + propertyName, ex); 1062 } 1063 } 1064 } 1065 1066 1067 /** 1068 * Equivalent to <code>setFieldsOn(cl, false)</code>. 1069 * 1070 * @see #setFieldsOn(Class, boolean) 1071 * 1072 * @hide 1073 */ 1074 public static void setFieldsOn(Class<?> cl) { 1075 setFieldsOn(cl, false); 1076 } 1077 1078 /** 1079 * Reflectively sets static fields of a class based on internal debugging 1080 * properties. This method is a no-op if android.util.Config.DEBUG is 1081 * false. 1082 * <p> 1083 * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: Config.DEBUG will 1084 * always be false in release builds. This API is typically only useful 1085 * for platform developers. 1086 * </p> 1087 * Class setup: define a class whose only fields are non-final, static 1088 * primitive types (except for "char") or Strings. In a static block 1089 * after the field definitions/initializations, pass the class to 1090 * this method, Debug.setFieldsOn(). Example: 1091 * <pre> 1092 * package com.example; 1093 * 1094 * import android.os.Debug; 1095 * 1096 * public class MyDebugVars { 1097 * public static String s = "a string"; 1098 * public static String s2 = "second string"; 1099 * public static String ns = null; 1100 * public static boolean b = false; 1101 * public static int i = 5; 1102 * @Debug.DebugProperty 1103 * public static float f = 0.1f; 1104 * @@Debug.DebugProperty 1105 * public static double d = 0.5d; 1106 * 1107 * // This MUST appear AFTER all fields are defined and initialized! 1108 * static { 1109 * // Sets all the fields 1110 * Debug.setFieldsOn(MyDebugVars.class); 1111 * 1112 * // Sets only the fields annotated with @Debug.DebugProperty 1113 * // Debug.setFieldsOn(MyDebugVars.class, true); 1114 * } 1115 * } 1116 * </pre> 1117 * setFieldsOn() may override the value of any field in the class based 1118 * on internal properties that are fixed at boot time. 1119 * <p> 1120 * These properties are only set during platform debugging, and are not 1121 * meant to be used as a general-purpose properties store. 1122 * 1123 * {@hide} 1124 * 1125 * @param cl The class to (possibly) modify 1126 * @param partial If false, sets all static fields, otherwise, only set 1127 * fields with the {@link android.os.Debug.DebugProperty} 1128 * annotation 1129 * @throws IllegalArgumentException if any fields are final or non-static, 1130 * or if the type of the field does not match the type of 1131 * the internal debugging property value. 1132 */ 1133 public static void setFieldsOn(Class<?> cl, boolean partial) { 1134 if (Config.DEBUG) { 1135 if (debugProperties != null) { 1136 /* Only look for fields declared directly by the class, 1137 * so we don't mysteriously change static fields in superclasses. 1138 */ 1139 for (Field field : cl.getDeclaredFields()) { 1140 if (!partial || field.getAnnotation(DebugProperty.class) != null) { 1141 final String propertyName = cl.getName() + "." + field.getName(); 1142 boolean isStatic = Modifier.isStatic(field.getModifiers()); 1143 boolean isFinal = Modifier.isFinal(field.getModifiers()); 1144 1145 if (!isStatic || isFinal) { 1146 throw new IllegalArgumentException(propertyName + 1147 " must be static and non-final"); 1148 } 1149 modifyFieldIfSet(field, debugProperties, propertyName); 1150 } 1151 } 1152 } 1153 } else { 1154 Log.wtf(TAG, 1155 "setFieldsOn(" + (cl == null ? "null" : cl.getName()) + 1156 ") called in non-DEBUG build"); 1157 } 1158 } 1159 1160 /** 1161 * Annotation to put on fields you want to set with 1162 * {@link Debug#setFieldsOn(Class, boolean)}. 1163 * 1164 * @hide 1165 */ 1166 @Target({ ElementType.FIELD }) 1167 @Retention(RetentionPolicy.RUNTIME) 1168 public @interface DebugProperty { 1169 } 1170 1171 /** 1172 * Get a debugging dump of a system service by name. 1173 * 1174 * <p>Most services require the caller to hold android.permission.DUMP. 1175 * 1176 * @param name of the service to dump 1177 * @param fd to write dump output to (usually an output log file) 1178 * @param args to pass to the service's dump method, may be null 1179 * @return true if the service was dumped successfully, false if 1180 * the service could not be found or had an error while dumping 1181 */ 1182 public static boolean dumpService(String name, FileDescriptor fd, String[] args) { 1183 IBinder service = ServiceManager.getService(name); 1184 if (service == null) { 1185 Log.e(TAG, "Can't find service to dump: " + name); 1186 return false; 1187 } 1188 1189 try { 1190 service.dump(fd, args); 1191 return true; 1192 } catch (RemoteException e) { 1193 Log.e(TAG, "Can't dump service: " + name, e); 1194 return false; 1195 } 1196 } 1197} 1198