Debug.java revision 798e2d3d0c80e269ead1489dbc3f2f762f1e52b5
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 * Count the number and aggregate size of memory allocations between 517 * two points. 518 * 519 * The "start" function resets the counts and enables counting. The 520 * "stop" function disables the counting so that the analysis code 521 * doesn't cause additional allocations. The "get" function returns 522 * the specified value. 523 * 524 * 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. 527 */ 528 public static void startAllocCounting() { 529 VMDebug.startAllocCounting(); 530 } 531 public static void stopAllocCounting() { 532 VMDebug.stopAllocCounting(); 533 } 534 535 public static int getGlobalAllocCount() { 536 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS); 537 } 538 public static int getGlobalAllocSize() { 539 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES); 540 } 541 public static int getGlobalFreedCount() { 542 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS); 543 } 544 public static int getGlobalFreedSize() { 545 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES); 546 } 547 public static int getGlobalClassInitCount() { 548 /* number of classes that have been successfully initialized */ 549 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT); 550 } 551 public static int getGlobalClassInitTime() { 552 /* cumulative elapsed time for class initialization, in usec */ 553 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME); 554 } 555 public static int getGlobalExternalAllocCount() { 556 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_OBJECTS); 557 } 558 public static int getGlobalExternalAllocSize() { 559 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_BYTES); 560 } 561 public static int getGlobalExternalFreedCount() { 562 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_OBJECTS); 563 } 564 public static int getGlobalExternalFreedSize() { 565 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_BYTES); 566 } 567 public static int getGlobalGcInvocationCount() { 568 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS); 569 } 570 public static int getThreadAllocCount() { 571 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS); 572 } 573 public static int getThreadAllocSize() { 574 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES); 575 } 576 public static int getThreadExternalAllocCount() { 577 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_OBJECTS); 578 } 579 public static int getThreadExternalAllocSize() { 580 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_BYTES); 581 } 582 public static int getThreadGcInvocationCount() { 583 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS); 584 } 585 586 public static void resetGlobalAllocCount() { 587 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS); 588 } 589 public static void resetGlobalAllocSize() { 590 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES); 591 } 592 public static void resetGlobalFreedCount() { 593 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS); 594 } 595 public static void resetGlobalFreedSize() { 596 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES); 597 } 598 public static void resetGlobalClassInitCount() { 599 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT); 600 } 601 public static void resetGlobalClassInitTime() { 602 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME); 603 } 604 public static void resetGlobalExternalAllocCount() { 605 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_OBJECTS); 606 } 607 public static void resetGlobalExternalAllocSize() { 608 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_ALLOCATED_BYTES); 609 } 610 public static void resetGlobalExternalFreedCount() { 611 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_OBJECTS); 612 } 613 public static void resetGlobalExternalFreedSize() { 614 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_EXT_FREED_BYTES); 615 } 616 public static void resetGlobalGcInvocationCount() { 617 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS); 618 } 619 public static void resetThreadAllocCount() { 620 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS); 621 } 622 public static void resetThreadAllocSize() { 623 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES); 624 } 625 public static void resetThreadExternalAllocCount() { 626 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_OBJECTS); 627 } 628 public static void resetThreadExternalAllocSize() { 629 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_EXT_ALLOCATED_BYTES); 630 } 631 public static void resetThreadGcInvocationCount() { 632 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS); 633 } 634 public static void resetAllCounts() { 635 VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS); 636 } 637 638 /** 639 * Returns the size of the native heap. 640 * @return The size of the native heap in bytes. 641 */ 642 public static native long getNativeHeapSize(); 643 644 /** 645 * Returns the amount of allocated memory in the native heap. 646 * @return The allocated size in bytes. 647 */ 648 public static native long getNativeHeapAllocatedSize(); 649 650 /** 651 * Returns the amount of free memory in the native heap. 652 * @return The freed size in bytes. 653 */ 654 public static native long getNativeHeapFreeSize(); 655 656 /** 657 * Retrieves information about this processes memory usages. This information is broken down by 658 * how much is in use by dalivk, the native heap, and everything else. 659 */ 660 public static native void getMemoryInfo(MemoryInfo memoryInfo); 661 662 /** 663 * Note: currently only works when the requested pid has the same UID 664 * as the caller. 665 * @hide 666 */ 667 public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo); 668 669 /** 670 * Establish an object allocation limit in the current thread. Useful 671 * for catching regressions in code that is expected to operate 672 * without causing any allocations. 673 * 674 * Pass in the maximum number of allowed allocations. Use -1 to disable 675 * the limit. Returns the previous limit. 676 * 677 * The preferred way to use this is: 678 * 679 * int prevLimit = -1; 680 * try { 681 * prevLimit = Debug.setAllocationLimit(0); 682 * ... do stuff that's not expected to allocate memory ... 683 * } finally { 684 * Debug.setAllocationLimit(prevLimit); 685 * } 686 * 687 * This allows limits to be nested. The try/finally ensures that the 688 * limit is reset if something fails. 689 * 690 * Exceeding the limit causes a dalvik.system.AllocationLimitError to 691 * be thrown from a memory allocation call. The limit is reset to -1 692 * when this happens. 693 * 694 * The feature may be disabled in the VM configuration. If so, this 695 * call has no effect, and always returns -1. 696 */ 697 public static int setAllocationLimit(int limit) { 698 return VMDebug.setAllocationLimit(limit); 699 } 700 701 /** 702 * Establish a global object allocation limit. This is similar to 703 * {@link #setAllocationLimit(int)} but applies to all threads in 704 * the VM. It will coexist peacefully with per-thread limits. 705 * 706 * [ The value of "limit" is currently restricted to 0 (no allocations 707 * allowed) or -1 (no global limit). This may be changed in a future 708 * release. ] 709 */ 710 public static int setGlobalAllocationLimit(int limit) { 711 if (limit != 0 && limit != -1) 712 throw new IllegalArgumentException("limit must be 0 or -1"); 713 return VMDebug.setGlobalAllocationLimit(limit); 714 } 715 716 /** 717 * Dump a list of all currently loaded class to the log file. 718 * 719 * @param flags See constants above. 720 */ 721 public static void printLoadedClasses(int flags) { 722 VMDebug.printLoadedClasses(flags); 723 } 724 725 /** 726 * Get the number of loaded classes. 727 * @return the number of loaded classes. 728 */ 729 public static int getLoadedClassCount() { 730 return VMDebug.getLoadedClassCount(); 731 } 732 733 /** 734 * Dump "hprof" data to the specified file. This will cause a GC. 735 * 736 * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof"). 737 * @throws UnsupportedOperationException if the VM was built without 738 * HPROF support. 739 * @throws IOException if an error occurs while opening or writing files. 740 */ 741 public static void dumpHprofData(String fileName) throws IOException { 742 VMDebug.dumpHprofData(fileName); 743 } 744 745 /** 746 * Collect "hprof" and send it to DDMS. This will cause a GC. 747 * 748 * @throws UnsupportedOperationException if the VM was built without 749 * HPROF support. 750 * 751 * @hide 752 */ 753 public static void dumpHprofDataDdms() { 754 VMDebug.dumpHprofDataDdms(); 755 } 756 757 /** 758 * Returns the number of sent transactions from this process. 759 * @return The number of sent transactions or -1 if it could not read t. 760 */ 761 public static native int getBinderSentTransactions(); 762 763 /** 764 * Returns the number of received transactions from the binder driver. 765 * @return The number of received transactions or -1 if it could not read the stats. 766 */ 767 public static native int getBinderReceivedTransactions(); 768 769 /** 770 * Returns the number of active local Binder objects that exist in the 771 * current process. 772 */ 773 public static final native int getBinderLocalObjectCount(); 774 775 /** 776 * Returns the number of references to remote proxy Binder objects that 777 * exist in the current process. 778 */ 779 public static final native int getBinderProxyObjectCount(); 780 781 /** 782 * Returns the number of death notification links to Binder objects that 783 * exist in the current process. 784 */ 785 public static final native int getBinderDeathObjectCount(); 786 787 /** 788 * Primes the register map cache. 789 * 790 * Only works for classes in the bootstrap class loader. Does not 791 * cause classes to be loaded if they're not already present. 792 * 793 * The classAndMethodDesc argument is a concatentation of the VM-internal 794 * class descriptor, method name, and method descriptor. Examples: 795 * Landroid/os/Looper;.loop:()V 796 * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V 797 * 798 * @param classAndMethodDesc the method to prepare 799 * 800 * @hide 801 */ 802 public static final boolean cacheRegisterMap(String classAndMethodDesc) { 803 return VMDebug.cacheRegisterMap(classAndMethodDesc); 804 } 805 806 /** 807 * Dumps the contents of VM reference tables (e.g. JNI locals and 808 * globals) to the log file. 809 * 810 * @hide 811 */ 812 public static final void dumpReferenceTables() { 813 VMDebug.dumpReferenceTables(); 814 } 815 816 /** 817 * API for gathering and querying instruction counts. 818 * 819 * Example usage: 820 * Debug.InstructionCount icount = new Debug.InstructionCount(); 821 * icount.resetAndStart(); 822 * [... do lots of stuff ...] 823 * if (icount.collect()) { 824 * System.out.println("Total instructions executed: " 825 * + icount.globalTotal()); 826 * System.out.println("Method invocations: " 827 * + icount.globalMethodInvocations()); 828 * } 829 */ 830 public static class InstructionCount { 831 private static final int NUM_INSTR = 256; 832 833 private int[] mCounts; 834 835 public InstructionCount() { 836 mCounts = new int[NUM_INSTR]; 837 } 838 839 /** 840 * Reset counters and ensure counts are running. Counts may 841 * have already been running. 842 * 843 * @return true if counting was started 844 */ 845 public boolean resetAndStart() { 846 try { 847 VMDebug.startInstructionCounting(); 848 VMDebug.resetInstructionCount(); 849 } catch (UnsupportedOperationException uoe) { 850 return false; 851 } 852 return true; 853 } 854 855 /** 856 * Collect instruction counts. May or may not stop the 857 * counting process. 858 */ 859 public boolean collect() { 860 try { 861 VMDebug.stopInstructionCounting(); 862 VMDebug.getInstructionCount(mCounts); 863 } catch (UnsupportedOperationException uoe) { 864 return false; 865 } 866 return true; 867 } 868 869 /** 870 * Return the total number of instructions executed globally (i.e. in 871 * all threads). 872 */ 873 public int globalTotal() { 874 int count = 0; 875 for (int i = 0; i < NUM_INSTR; i++) 876 count += mCounts[i]; 877 return count; 878 } 879 880 /** 881 * Return the total number of method-invocation instructions 882 * executed globally. 883 */ 884 public int globalMethodInvocations() { 885 int count = 0; 886 887 //count += mCounts[Opcodes.OP_EXECUTE_INLINE]; 888 count += mCounts[Opcodes.OP_INVOKE_VIRTUAL]; 889 count += mCounts[Opcodes.OP_INVOKE_SUPER]; 890 count += mCounts[Opcodes.OP_INVOKE_DIRECT]; 891 count += mCounts[Opcodes.OP_INVOKE_STATIC]; 892 count += mCounts[Opcodes.OP_INVOKE_INTERFACE]; 893 count += mCounts[Opcodes.OP_INVOKE_VIRTUAL_RANGE]; 894 count += mCounts[Opcodes.OP_INVOKE_SUPER_RANGE]; 895 count += mCounts[Opcodes.OP_INVOKE_DIRECT_RANGE]; 896 count += mCounts[Opcodes.OP_INVOKE_STATIC_RANGE]; 897 count += mCounts[Opcodes.OP_INVOKE_INTERFACE_RANGE]; 898 //count += mCounts[Opcodes.OP_INVOKE_DIRECT_EMPTY]; 899 count += mCounts[Opcodes.OP_INVOKE_VIRTUAL_QUICK]; 900 count += mCounts[Opcodes.OP_INVOKE_VIRTUAL_QUICK_RANGE]; 901 count += mCounts[Opcodes.OP_INVOKE_SUPER_QUICK]; 902 count += mCounts[Opcodes.OP_INVOKE_SUPER_QUICK_RANGE]; 903 return count; 904 } 905 } 906 907 908 /** 909 * A Map of typed debug properties. 910 */ 911 private static final TypedProperties debugProperties; 912 913 /* 914 * Load the debug properties from the standard files into debugProperties. 915 */ 916 static { 917 if (Config.DEBUG) { 918 final String TAG = "DebugProperties"; 919 final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" }; 920 final TypedProperties tp = new TypedProperties(); 921 922 // Read the properties from each of the files, if present. 923 for (String file : files) { 924 Reader r; 925 try { 926 r = new FileReader(file); 927 } catch (FileNotFoundException ex) { 928 // It's ok if a file is missing. 929 continue; 930 } 931 932 try { 933 tp.load(r); 934 } catch (Exception ex) { 935 throw new RuntimeException("Problem loading " + file, ex); 936 } finally { 937 try { 938 r.close(); 939 } catch (IOException ex) { 940 // Ignore this error. 941 } 942 } 943 } 944 945 debugProperties = tp.isEmpty() ? null : tp; 946 } else { 947 debugProperties = null; 948 } 949 } 950 951 952 /** 953 * Returns true if the type of the field matches the specified class. 954 * Handles the case where the class is, e.g., java.lang.Boolean, but 955 * the field is of the primitive "boolean" type. Also handles all of 956 * the java.lang.Number subclasses. 957 */ 958 private static boolean fieldTypeMatches(Field field, Class<?> cl) { 959 Class<?> fieldClass = field.getType(); 960 if (fieldClass == cl) { 961 return true; 962 } 963 Field primitiveTypeField; 964 try { 965 /* All of the classes we care about (Boolean, Integer, etc.) 966 * have a Class field called "TYPE" that points to the corresponding 967 * primitive class. 968 */ 969 primitiveTypeField = cl.getField("TYPE"); 970 } catch (NoSuchFieldException ex) { 971 return false; 972 } 973 try { 974 return fieldClass == (Class<?>) primitiveTypeField.get(null); 975 } catch (IllegalAccessException ex) { 976 return false; 977 } 978 } 979 980 981 /** 982 * Looks up the property that corresponds to the field, and sets the field's value 983 * if the types match. 984 */ 985 private static void modifyFieldIfSet(final Field field, final TypedProperties properties, 986 final String propertyName) { 987 if (field.getType() == java.lang.String.class) { 988 int stringInfo = properties.getStringInfo(propertyName); 989 switch (stringInfo) { 990 case TypedProperties.STRING_SET: 991 // Handle as usual below. 992 break; 993 case TypedProperties.STRING_NULL: 994 try { 995 field.set(null, null); // null object for static fields; null string 996 } catch (IllegalAccessException ex) { 997 throw new IllegalArgumentException( 998 "Cannot set field for " + propertyName, ex); 999 } 1000 return; 1001 case TypedProperties.STRING_NOT_SET: 1002 return; 1003 case TypedProperties.STRING_TYPE_MISMATCH: 1004 throw new IllegalArgumentException( 1005 "Type of " + propertyName + " " + 1006 " does not match field type (" + field.getType() + ")"); 1007 default: 1008 throw new IllegalStateException( 1009 "Unexpected getStringInfo(" + propertyName + ") return value " + 1010 stringInfo); 1011 } 1012 } 1013 Object value = properties.get(propertyName); 1014 if (value != null) { 1015 if (!fieldTypeMatches(field, value.getClass())) { 1016 throw new IllegalArgumentException( 1017 "Type of " + propertyName + " (" + value.getClass() + ") " + 1018 " does not match field type (" + field.getType() + ")"); 1019 } 1020 try { 1021 field.set(null, value); // null object for static fields 1022 } catch (IllegalAccessException ex) { 1023 throw new IllegalArgumentException( 1024 "Cannot set field for " + propertyName, ex); 1025 } 1026 } 1027 } 1028 1029 1030 /** 1031 * Equivalent to <code>setFieldsOn(cl, false)</code>. 1032 * 1033 * @see #setFieldsOn(Class, boolean) 1034 * 1035 * @hide 1036 */ 1037 public static void setFieldsOn(Class<?> cl) { 1038 setFieldsOn(cl, false); 1039 } 1040 1041 /** 1042 * Reflectively sets static fields of a class based on internal debugging 1043 * properties. This method is a no-op if android.util.Config.DEBUG is 1044 * false. 1045 * <p> 1046 * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: Config.DEBUG will 1047 * always be false in release builds. This API is typically only useful 1048 * for platform developers. 1049 * </p> 1050 * Class setup: define a class whose only fields are non-final, static 1051 * primitive types (except for "char") or Strings. In a static block 1052 * after the field definitions/initializations, pass the class to 1053 * this method, Debug.setFieldsOn(). Example: 1054 * <pre> 1055 * package com.example; 1056 * 1057 * import android.os.Debug; 1058 * 1059 * public class MyDebugVars { 1060 * public static String s = "a string"; 1061 * public static String s2 = "second string"; 1062 * public static String ns = null; 1063 * public static boolean b = false; 1064 * public static int i = 5; 1065 * @Debug.DebugProperty 1066 * public static float f = 0.1f; 1067 * @@Debug.DebugProperty 1068 * public static double d = 0.5d; 1069 * 1070 * // This MUST appear AFTER all fields are defined and initialized! 1071 * static { 1072 * // Sets all the fields 1073 * Debug.setFieldsOn(MyDebugVars.class); 1074 * 1075 * // Sets only the fields annotated with @Debug.DebugProperty 1076 * // Debug.setFieldsOn(MyDebugVars.class, true); 1077 * } 1078 * } 1079 * </pre> 1080 * setFieldsOn() may override the value of any field in the class based 1081 * on internal properties that are fixed at boot time. 1082 * <p> 1083 * These properties are only set during platform debugging, and are not 1084 * meant to be used as a general-purpose properties store. 1085 * 1086 * {@hide} 1087 * 1088 * @param cl The class to (possibly) modify 1089 * @param partial If false, sets all static fields, otherwise, only set 1090 * fields with the {@link android.os.Debug.DebugProperty} 1091 * annotation 1092 * @throws IllegalArgumentException if any fields are final or non-static, 1093 * or if the type of the field does not match the type of 1094 * the internal debugging property value. 1095 */ 1096 public static void setFieldsOn(Class<?> cl, boolean partial) { 1097 if (Config.DEBUG) { 1098 if (debugProperties != null) { 1099 /* Only look for fields declared directly by the class, 1100 * so we don't mysteriously change static fields in superclasses. 1101 */ 1102 for (Field field : cl.getDeclaredFields()) { 1103 if (!partial || field.getAnnotation(DebugProperty.class) != null) { 1104 final String propertyName = cl.getName() + "." + field.getName(); 1105 boolean isStatic = Modifier.isStatic(field.getModifiers()); 1106 boolean isFinal = Modifier.isFinal(field.getModifiers()); 1107 1108 if (!isStatic || isFinal) { 1109 throw new IllegalArgumentException(propertyName + 1110 " must be static and non-final"); 1111 } 1112 modifyFieldIfSet(field, debugProperties, propertyName); 1113 } 1114 } 1115 } 1116 } else { 1117 Log.wtf(TAG, 1118 "setFieldsOn(" + (cl == null ? "null" : cl.getName()) + 1119 ") called in non-DEBUG build"); 1120 } 1121 } 1122 1123 /** 1124 * Annotation to put on fields you want to set with 1125 * {@link Debug#setFieldsOn(Class, boolean)}. 1126 * 1127 * @hide 1128 */ 1129 @Target({ ElementType.FIELD }) 1130 @Retention(RetentionPolicy.RUNTIME) 1131 public @interface DebugProperty { 1132 } 1133 1134 /** 1135 * Get a debugging dump of a system service by name. 1136 * 1137 * <p>Most services require the caller to hold android.permission.DUMP. 1138 * 1139 * @param name of the service to dump 1140 * @param fd to write dump output to (usually an output log file) 1141 * @param args to pass to the service's dump method, may be null 1142 * @return true if the service was dumped successfully, false if 1143 * the service could not be found or had an error while dumping 1144 */ 1145 public static boolean dumpService(String name, FileDescriptor fd, String[] args) { 1146 IBinder service = ServiceManager.getService(name); 1147 if (service == null) { 1148 Log.e(TAG, "Can't find service to dump: " + name); 1149 return false; 1150 } 1151 1152 try { 1153 service.dump(fd, args); 1154 return true; 1155 } catch (RemoteException e) { 1156 Log.e(TAG, "Can't dump service: " + name, e); 1157 return false; 1158 } 1159 } 1160} 1161